Crack Scala Interviews: 3 Questions Every Spark/Scala Engineer Should Know
Briefly

Crack Scala Interviews: 3 Questions Every Spark/Scala Engineer Should Know
"šŸ”¹ Q1: Find the First Occurrence of a Substring Problem:Given two strings needle and haystack, return the index of the first occurrence of needle in haystack. If needle is not found, return -1. Example: Input: haystack = "sadbutsad", needle = "sad" → Output: 0 Input: haystack = "HelloWorld", needle = "HelloHi" → Output: -1 āœ… Scala Solution object FirstOccurrence { def strStr(haystack: String, needle: String): Int = { if (needle.isEmpty) return 0 haystack.indexOf(needle) } def main(args: Array[String]): Unit = { println(strStr("sadbutsad", "sad")) // 0 println(strStr("HelloWorld", "HelloHi")) // -1 }} šŸ’” Explanation Scala's indexOf method is efficient and directly gives the first match. Returning -1 when not found aligns with expected behavior. šŸ‘‰ What the interviewer learns:This checks whether you know string operations in Scala, can think about edge cases (empty strings), and prefer built-in idiomatic solutions instead of reinventing the wheel."
"šŸ”¹ Q2: Sort an Array of 0s and 1s Problem:Given an array containing only 0s and 1s in random order, sort it. Example: Input: [0, 1, 0, 1, 0, 0, 1, 1, 1, 0] Output: [0, 0, 0, 0, 0, 1, 1, 1, 1, 1] āœ… Scala Solution object BinarySort { def sortBinaryArray(arr: Array[Int]): Array[Int] = { arr.sorted } def main(args: Array[String]): Unit = { val input = Array(0, 1, 0, 1, 0, 0, 1, 1, 1, 0) println(sortBinaryArray(input).mkString(", ")) }} šŸ’” Explanation arr.sorted leverages Scala's collection API to perform sorting cleanly. But since the input contains only 0s and 1s, a more optimal approach is to simpl"
Three Scala interview-style problems cover string manipulation, functional collections, and stack-based problem solving. The first problem returns the index of the first occurrence of a needle in a haystack, handling empty-needle edge cases and using Scala's indexOf to return 0 or -1. The second problem sorts an array containing only 0s and 1s; the provided solution uses arr.sorted while noting that counting zeros and ones would be more optimal for binary inputs. A third stack-based problem is referenced as part of the set but its details are not included.
Read at Medium
Unable to calculate read time
[
|
]