Introduction:
Hello everyone, Scala programming questions are crucial for preparing for big data interviews. In today’s post, I’ll cover important Scala programming questions that can help you get ready for your big data interview.
Questions:
1. Write a Program in Scala for fibonacci Series.
2. Write a Program to Find a Factorial of Given number using Tail Recursion.
3. Write a Program to find a string is palindrome or not?
4. Write a Program to find a NUMBER is palindrome or not?
5. Write a Program to find Given List is Palindrome or Not.?
6. Write a Program to Merge two Unsorted List and return sorted list without duplicates?
7. Write a word count program without using Spark.
8. Write a program to find Number of Values which are perfect square?
9. Explain Option with example and write its usage.
10. Explain Higher order function with an example.
11. Explain Function currying with an example.
12. Explain clouser with example?
13. Explain Pattern matching and case class with an example.
14. Write a Scala program to find smallest and second smallest elements of a given array.
15. Write a program to find smallest and largest kth elements in List
16. What is a partially applied function in Scala explain with example?
17. Explain pure Function With Example.
18. Explain Anonymous Function with Example:
19. Explain Concept of monad with Example.
20. Describe Exception Handling with example in Scala?
- Write a Program in Scala for fibonacci Series.
def fibonacci(n: Int): Int =
if (n < 3) 1
else
fibonacci(n - 1) + fibonacci(n - 2)
2. Write a Program to Find a Factorial of Given number using Tail Recursion.
def facttail(a: Int, result: Int): Int = {
if (a == 1)
result
else
facttail(a - 1, result * a)
}
facttail(4, 1)
3. Write a Program to find a string is palindrome or not?
def Stringpalindrome(x: String): String = {
var rev: String = ""
if (x.length > 1) {
rev = x.reverse
}
if (rev == x) return ("STRING is a palindrome")
else return ("STRING is not a palindrome")
}
4. Write a Program to find a NUMBER is palindrome or not?
def Numberpalindrome(x: Int): String = {
var rev: Int = 0
rev = x.toString.reverse.toInt
if (rev == x)
return ("Number is a palindrome")
else
return ("Number is not a palindrome")
}
5. Write a Program to find Given List is Palindrome or Not.?
def ListPalindrome[A](list_elements: List[A]): Boolean = {
list_elements == list_elements.reverse
}
6. Write a Program to Merge two Unsorted List and return sorted list without duplicates?
val l1 = List(1, 3, 5, 7, 11, 9)
val l3 = List(2, 4, 6, 8, 12, 10)
val l2 = l1 ::: l3
val l4 = l2.toSet.toList
val l5 = l4.sortWith(_ < _)
val l6 = l5.filter(_ != 1)
7. Write a word count program without using Spark.
val list = List(
"Rahul Patidar",
"Bangalore Pune ",
"BigData Spark Scala Hive Hadoop Rahul Patidar "
)
val words = list.flatMap(line => line.split(" "))
val mapData = words.map(word => (word, 1))
val groupedmapData = mapData.groupBy(_._1)
val result = groupedmapData.view.mapValues(list => {
list.map(_._2).sum
})
result.foreach(println)
8. Write a program to find Number of Values which are perfect square?
val numInput: Int = readInt()
val numInputValue: String = readLine()
val numInputValue_Split: Array[Int] = numInputValue.split(" ").map(_.toInt)
var count = 0
for (i <- numInputValue_Split) {
val sqrt = Math.sqrt(i)
if (sqrt.ceil - sqrt == 0) {
count = count + 1
}
}
9. Explain Option with example and write its usage.
The Scala Option[T] is referred to as a carrier of one element or none for a specified type. As the name suggests, this container holds either a Some or a None object, which represents a missing value. It holds Some[T] if a value is stored, otherwise none. In simple words, Scala options are wrappers for missing values.
val employee = Map(
"Rahul" -> "IBM",
"Umesh" -> "Jio",
"Rohit" -> "Microsoft",
"Raj" -> "Google"
)
val a = employee.get("Umesh")
val b = employee.get("Rohit")
val c = employee.get("Rahul")
val d = employee.get("Raj")
val e = employee.get("Avinash")
println(a);
println(b);
println(c);
println(d);
println(e);
10. Explain Higher order function with an example.
A higher-order function is a function that can take other functions as arguments, return a function as its result, or both. Higher-order functions are a powerful feature in functional programming languages like Scala.
var a = 1 to 10
def d1(i: Int): Int = {
i * i
}
a.map(d1)
11. Explain Function currying with an example.
Function currying is a technique where a function with multiple arguments is transformed into a series of functions, each taking a single argument.
Imagine you are in a coffee shop and want to create a specific order for different sizes and types of coffee. Instead of specifying both the size and type of coffee at the same time, you can decide the size first and then decide the type of coffee. This is similar to what currying does with functions.
Think of a non-curried function as ordering coffee by specifying both size and type at the same time:
def price(size: String, coffeeType: String): Double = {
(size, coffeeType) match {
case ("small", "latte") => 3.0
case ("medium", "latte") => 4.0
case ("large", "latte") => 5.0
case ("small", "espresso") => 2.5
case ("medium", "espresso") => 3.5
case ("large", "espresso") => 4.5
case _ => 0.0
}
}// Example usage:
val smallLattePrice = price("small", "latte")
println(smallLattePrice) // Output: 3.0
With currying, you first specify the size and then specify the type of coffee in two steps:
def priceCurried(size: String)(coffeeType: String): Double = {
(size, coffeeType) match {
case ("small", "latte") => 3.0
case ("medium", "latte") => 4.0
case ("large", "latte") => 5.0
case ("small", "espresso") => 2.5
case ("medium", "espresso") => 3.5
case ("large", "espresso") => 4.5
case _ => 0.0
}
}// Example usage:
val smallPrice = priceCurried("small") _
val smallLattePrice = smallPrice("latte")
println(smallLattePrice) // Output: 3.0
- Flexibility: You can create specific functions based on the first argument.
val smallPrice = priceCurried("small") _
val largePrice = priceCurried("large") _
- Now,
smallPrice
is a function that only needs the type of coffee to give the price for a small coffee. - Similarly,
largePrice
gives the price for a large coffee. - Reusability: The partially applied functions can be reused multiple times.
println(smallPrice("latte")) // Output: 3.0
println(smallPrice("espresso")) // Output: 2.5
println(largePrice("latte")) // Output: 5.0
def curryfun(x: Int, y: Int, f: Int => Int): Int = {
f(x) + f(y)
}
curryfun(4, 5, x => x * x * x)
12. Explain Scala Closures with example?
Scala Closures are functions which use one or more free variables and the return value of this function is dependent on these variables.
def clouser1 = {
val pi = 3.14;
(x: Int) => pi * x * x
}
val pi = 3.5
clouser1(10)
val c = clouser1
c(10)
13. Explain Pattern matching and case class with an example.
Pattern Matching: The matching method is used instead of a switch statement.The match method takes a number of cases as an argument. Each alternative takes a pattern and one or more expressions that will be performed if the pattern matches. A symbol => is used to separate the pattern from the expressions.
def patternmatchValue(i: Int): String = i match {
case 1 => "Accenture"
case 2 => "IBM"
case 3 => "Tangoe"
case 4 => "Jio"
case _ => "Unknown"
}
println(patternmatchValue(2))
Case Classes : Case Classes are Special Kind of Classes, Where You need to Write Less code. Case Class Parameters are automatically promoted to fields no need to add additional val in-class arguments. Case Classes are sensible to String: if we use toString(p1.toString) then it will give some reference value in the normal class but will give actual values in the case class.
case class person1(name: String, Age: Int)
val p1 = new person1("Rahul", 28)
equal and hashcode method already implemented In case Class.
In Normal Class when we use == then it will compare reference and it will Give False but in the case of Case class, it will Check the value and will give true.
val p11 = new person1("Rahul", 28)
p1 == p11
Case Classes by Default has a Companion object.
val p12 = person1.apply("Rahul", 25)
If we don’t use here apply method, then also it will automatically take.
No need to give a new keyword as it uses internal companion Object
val p13 = person1("Rahul", 25)
copy method is already Available
val p14 = p13.copy()
While copying if we want to change something can do it easily
val p15 = p13.copy(Age=20)
case class person1(name: String, Age: Int)
val p1 = new person1("Rahul", 28)
val p11 = new person1("Rahul", 28)
p1 == p11
val p12 = person1.apply("Rahul", 25)
val p13 = person1("Rahul", 25)
val p14 = p13.copy()
val p15 = p13.copy(Age = 20)
Case classes are serialisable and can send to the network.
14. Write a Scala program to find smallest and second smallest elements of a given array.
val arr = Array(5, 6, -9, 6, 15, 4);
println("Original array:")
for (x <- arr) {
print(s"${x}, ")
}
var first_element, second_element, arr_size = arr.length;
/* Return if the array size less than two */
if (arr_size < 2) {
println("\nArray size less than two.");
} else {
first_element = Int.MaxValue
second_element = Int.MaxValue
for (i <- 0 to arr_size - 1) {
/* Update both first and second if current element is smaller than first. */
if (arr(i) < first_element) {
second_element = first_element;
first_element = arr(i);
}
/* Update second if arr[i] is between first and second
elements.*/
else if (arr(i) < second_element && arr(i) != first_element)
second_element = arr(i);
}
if (second_element == Integer.MAX_VALUE)
println("\nNo second smallest element.");
else
println(
s"\nThe smallest element is ${first_element} and second Smallest element is ${second_element}."
);
}
15. Write a program to find smallest and largest kth elements in List
val list = List(7, 2, 1, 6, 3)
val listSorted = list.distinct.sorted
val listSortedDesc = list.sorted(Ordering.Int.reverse)
println("largest element : " + listSorted.last)
println("smallest element : " + listSortedDesc.last)
println("2nd largest element : " + listSorted.take(4).last)
println("2nd smallest element : " + listSortedDesc.take(4).last)
println("3rd largest element : " + listSorted.take(3).last)
println("3rd smallest element : " + listSortedDesc.take(3).last)
println("4th largest element : " + listSorted.take(2).last)
println("4th smallest element : " + listSortedDesc.take(2).last)
16. What is a partially applied function in Scala explain with example?
A partially applied function in Scala is a function where you fix (or pre-fill) some of the arguments of an existing function. This creates a new function that requires fewer arguments.
def sum(x: Int, y: Int, z: Int) = x + y + z// Partially applying the first argument
val addFiveAndTen = sum(5, 10, _: Int)// Now `addFiveAndTen` is a function that expects one more argument
val result = addFiveAndTen(15)println(result) // Output: 5 + 10 + 15 = 30
A pure function is a function that depends only on its declared inputs and its internal algorithm to produce its output. It does not read any other values from “the outside world”.
Properties of Pure Function:
- Input solely determines the output (no external dependencies).
dollertoinr(5)
It’s not a pure Function because the value is not static it may change if the dollar price changes
2. The function doesn’t change its input values.
def fun1(a: Int): Int = {
val b = a + 1
return a
}
Not a pure function since we are updating the value.
3. There are no side effects , which means the functions only do whatever it is intended for. It doesn’t do anything extra.
def fun1(a: Int): Int = {
println("hello");
return a
}
Has a side effect and printing something unnecessary. i.e. println(“hello”); so not a pure Function.
All mathematical functions are pure.
If all the above properties are true then it’s a pure function.
If any Function fulfils all the above-mentioned Properties then we can consider it as a Pure Function.
18. Explain Anonymous Function with Example:
In Scala, An anonymous function is also known as a function literal. A function which does not contain a name is known as an anonymous function. An anonymous function provides a lightweight function definition. It is useful when we want to create an inline function. The anonymous function is mainly used with higher-order functions like map, reduce etc. where we can define only functionality.
var myfc1 = (str1: String, str2: String) => str1 + str2
var myfc2 = (_: String) + (_: String)
19. Explain Concept of monad with Example.
A monad is not a class or a trait it is just a concept. A monad is an Object that wraps another object in scala. The output of a calculation is at any step is the input to another calculation.
val List1 = List(1, 2, 3, 4)
val List2 = List(5, 6, 7, 8)
List1.flatMap { x =>
List2.map { y =>
x + y
}
}
20. Describe Exception Handling with example in Scala?
Answer: Exception handling in Scala is similar to Java except that there are no checked exceptions. To throw exceptions, we use throw new
import java.io.FileReader
import java.io.FileNotFoundException
import java.io.IOException
try {
val f = new FileReader("input.txt")
} catch {
case ex: FileNotFoundException => {
println("Missing file exception")
}case ex: IOException => {
println("IO Exception")
}
}
21. Given a string
text
that may contain alphanumeric characters, special characters, null values, and blanks, write a Scala program to:Filter and separate the alphanumeric characters into:
Unique numbers (sorted in ascending order).
Unique alphabets (sorted in ascending order).
Retain the entire sequence of alphabets in their original order.
Display the results as a list containing:
A string of sorted unique numbers.
A string of sorted unique alphabets.
The entire sequence of alphabets from the input.
import scala.io.StdIn.readLineobject TextProcessing {
def main(args: Array[String]): Unit = {
val text = readLine("Enter the text: ")
// Filter and separate characters into alphabets and numbers
val (alphabets, numbers) = text.partition(_.isLetter)
// Extract unique numbers and alphabets
val uniqueNumbers = numbers.distinct.filter(_.isDigit)
val uniqueAlphabets = alphabets.distinct.filter(_.isLetter)
// Convert the sets to sorted strings
val sortedUniqueNumbers = uniqueNumbers.sorted.mkString
val sortedUniqueAlphabets = uniqueAlphabets.sorted.mkString
// Prepare the output list
val output = List(sortedUniqueNumbers, sortedUniqueAlphabets, text)
// Print the result
println(output)
}
}
22. You have a dataset representing daily prices for a product named “KitKat”. Calculate the price difference between each day’s price and the previous day’s price.
import spark.implicits._
val data = Seq(
(1, "KitKat", 1000.0, "2021-01-01"),
(1, "KitKat", 2000.0, "2021-01-02"),
(1, "KitKat", 1000.0, "2021-01-03"),
(1, "KitKat", 2000.0, "2021-01-04"),
(1, "KitKat", 3000.0, "2021-01-05"),
(1, "KitKat", 1000.0, "2021-01-06")
)
val df = data.toDF("IT_ID", "IT_Name", "Price", "PriceDate")df.createOrReplaceTempView("price_data")
val priceDiffDF = spark.sql(
"""
|SELECT IT_ID, IT_Name, Price, PriceDate,
| Price - LAG(Price, 1, 0) OVER (PARTITION BY IT_ID
ORDER BY PriceDate) AS Price_Diff
|FROM price_data
|ORDER BY PriceDate
""".stripMargin)
// Show the resulting DataFrame
priceDiffDF.show()
Conclusion:In this article we have discussed the Most Important Scala programming Language Questions.I have also written one more article Scala Important Concept where I have covered Theoretical Question which are Important for Scala Interview.
Important Links For Data Engineers:
1. EveryThing You Need To Need About Scala.
2. Top 20 Scala Programming Question For Interview.
3. Spark Dataframes For Beginner.
4. A Complete Guide On Spark Structure Streaming.
5. A Complete Guide On Apache Hive Basics.
6. A Complete Guide On Apache Hive Advance.
7. Hadoop Components Installation Guide In MacOs
8. Slowly Changing Dimension In Hive — DataWareHouse.
9. A Comprehensive Guide On Apache Sqoop
10. Linux Commands You’ll Actually Need for Your Data-Engineering Journey.
11. Apache Spark Checkpoints: Let’s Recover Lost Data From Offset Values.
Happy Learning!!
Note: To discover the best big-data blogs, visit Feedspot. This is the best Big Data blog list curated from thousands of blogs on the web and ranked by traffic, social media followers, domain authority, and freshness.