Scala Coding Interview Checklist

Scala Coding Interview Checklist

Scala can make interview solutions short and elegant—or turn a simple problem into a puzzle of implicit conversions and chained combinators that nobody can follow. If you're using Scala in a coding round, the goal is code that's correct, efficient, and easy to explain. This Scala coding interview checklist covers the collections, idioms, and traps worth reviewing before the day.

First, confirm with your recruiter that Scala is allowed, which version (2.13 and 3 differ in syntax), and whether you can compile and run code.

1. Choose the right collection, on purpose

Scala gives you immutable collections by default. That's great for correctness but can hurt performance in tight loops if you're not careful.

NeedImmutableMutableInterview note
Indexed sequenceVectorArrayBuffer, ArrayList has O(n) indexing
Prepend-heavyList—:: is O(1)
Hash mapMapmutable.HashMapgetOrElse for defaults
SetSetmutable.HashSet
QueueQueuemutable.Queue, ArrayDequeFor BFS
StackListmutable.Stack, ArrayDeque
Priority queue—mutable.PriorityQueueMax-heap by default

Say your reasoning aloud: "I'll use a mutable ArrayDeque for BFS because the algorithm is naturally imperative, and keep the public function pure."

Mixing styles is fine in interviews as long as you explain why.

2. The List indexing trap

List is a singly linked list. list(i) and list.length are O(n). Inside a loop, that quietly turns O(n) into O(n²).

// Risky
for (i <- 0 until xs.length) { ... xs(i) ... }

// Better for random access
val arr = xs.toArray
for (i <- arr.indices) { ... arr(i) ... }

Mention it: "I'm converting to an array so indexing is constant time."

3. PriorityQueue ordering

mutable.PriorityQueue dequeues the largest element first. For a min-heap, reverse the ordering:

import scala.collection.mutable
val minHeap = mutable.PriorityQueue.empty[(Int, Int)](
  Ordering.by[(Int, Int), Int](_._1).reverse
)
minHeap.enqueue((dist, node))
val (d, u) = minHeap.dequeue()

Practice this once before the interview. Getting the ordering wrong is a common source of silent bugs in Dijkstra and top-k problems.

4. Option instead of null

Use Option and say why:

val idx: Option[Int] = seen.get(target - x)
idx match {
  case Some(j) => return Array(j, i)
  case None    => seen(x) = i
}

Handy methods: getOrElse, map, fold, foreach, isDefined. Avoid .get on an Option unless you can justify that it's present.

5. Recursion and tail calls

Scala supports recursion well, but deep recursion can still overflow the JVM stack. Use @tailrec when the recursive call is in tail position:

import scala.annotation.tailrec
@tailrec
def gcd(a: Int, b: Int): Int =
  if (b == 0) a else gcd(b, a % b)

For tree DFS, recursion depth equals tree height—fine for balanced trees, risky for a degenerate chain of 100,000 nodes. Mention an iterative fallback with an explicit stack if input size could be large.

6. Keep combinator chains readable

groupBy, map, foldLeft, sliding, and zipWithIndex are powerful. But a five-step chain is hard to debug live.

// Readable
val counts = s.groupBy(identity).view.mapValues(_.length).toMap

// Often clearer in an interview
val counts = mutable.HashMap.empty[Char, Int].withDefaultValue(0)
for (c <- s) counts(c) += 1

A good rule: if you can't explain the chain in one breath, break it into named steps.

7. Performance notes worth stating

8. Syntax you should have on autopilot

Common Scala interview mistakes

  1. Indexing a List inside a loop
  2. Forgetting PriorityQueue is a max-heap
  3. Calling .get on an Option without explanation
  4. Writing clever one-liners that you then can't debug
  5. Mixing Scala 2 and Scala 3 syntax without noticing
  6. Deep non-tail recursion on large input

Soft practice note

Idiomatic but explainable Scala takes rehearsal—especially saying your collection choices out loud. If you want a calm, structured way to practice before the real round, Luna Interview is built for rehearsal and prep, not as a way around the rules during an interview.

Day-before checklist

Scala's expressiveness is an advantage when it serves clarity. Write code the interviewer can read on the first pass, and explain each choice as you go.