-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Object-Oriented Scala
-
Section 4: Functional Scala
-
Section 5: Collections in Depth
-
Section 6: Type System
-
Section 7: Concurrency and Ecosystem
-
Section 8: Practical Projects
-
Section 9: Interview Practice
-
Section 10: Data Structures and Algorithms in Scala
-
Section 11: More Practice Exercises
-
Section 12: Advanced Functional Patterns
-
Section 13: More Ecosystem
-
Section 14: Scala Collections Library Deep Dive
-
Section 15: Scala Standard Library Deep Dive
-
Section 16: Akka Ecosystem Deep Dive
-
Section 17: Cats and Cats Effect Deep Dive
-
Section 18: Play Framework Deep Dive
-
Section 19: Apache Spark with Scala Deep Dive
-
Section 20: Scala Build Tools Deep Dive
-
Section 21: Scala 3 Specific Features
-
90: Union and Intersection Types
-
Section 22: Scala Testing Deep Dive
-
Section 23: Functional Domain Modeling
-
Section 24: More Data Structures and Algorithms in Scala
-
Section 25: Scala for Data Engineering
-
Section 26: More Practical Projects
-
Section 27: More Interview and Review
-
Section 28: ZIO Ecosystem Deep Dive
-
Section 29: Scala for Machine Learning
-
Section 30: Scala Microservices Architecture
-
Section 31: Scala Type System Deep Dive
-
Section 32: More Practice and Drills
-
Section 33: Scala Performance Deep Dive
-
Section 34: Scala Ecosystem Tooling
-
Section 35: Scala for Reactive Systems
-
Section 36: More Real-World Case Studies
-
Section 37: Scala for Financial Systems
-
Section 38: Scala GraphQL and gRPC
-
Section 39: More Final Projects
-
Section 40: More Interview and Final Review
-
Section 41: Scala for Streaming Data
-
Section 42: Scala Security Practices
-
Section 43: More Language Deep Dive
-
Section 44: Scala Command-Line Tools
-
Section 45: Scala Documentation and Style
-
Section 46: Scala Dependency Management
-
Section 47: More Practical Backend Patterns
-
Section 48: Scala for Event-Driven Architecture
-
Section 49: More Practice Drills Round 2
-
Section 50: Scala Compiler Deep Dive
-
Section 51: Scala for Web Frontends
-
Section 52: More Data Engineering Practice
-
Section 53: Scala Observability
-
Section 54: More Advanced Practice Projects
-
Section 55: Scala for Legacy Java Integration
-
Section 56: More Testing Practice
-
Section 57: Final Mastery Review
-
Section 58: Scala History and Ecosystem Context
-
Section 59: More Concurrency Patterns
-
Section 60: Scala for Configuration Management
-
Section 61: More Domain Modeling Practice
115: Whiteboard Practice: Implementing a Binary Tree Traversal Functionally
How should I define the tree structure to make pattern matching easy?
When you're at a whiteboard, don't overcomplicate the data model. You want a structure that mirrors the recursive nature of the traversal itself. In Scala, the gold standard here is a sealed trait. By sealing the trait, you tell the compiler that all possible subtypes are defined in this file, which means the compiler can warn you if your match statement is missing a case.
sealed trait BinaryTree
case class Node(value: Int, left: BinaryTree, right: BinaryTree) extends BinaryTree
case object Empty extends BinaryTree
I prefer this over using null or optional types. With this setup, the "base case" of your recursion is simply the Empty object. It makes the code read almost like a mathematical definition, which is exactly what interviewers are looking for when they ask for a "functional" implementation.
What's the cleanest way to implement an in-order traversal?
In-order traversal (Left, Root, Right) is the classic example because if the tree is a Binary Search Tree, it returns the elements in sorted order. The trick is to avoid using a mutable ListBuffer. Instead, embrace the recursive nature of the tree. You want each call to return a list, and then you concatenate them.
def inOrder(tree: BinaryTree): List[Int] = tree match {
case Empty => Nil
case Node(v, left, right) => inOrder(left) ++ List(v) ++ inOrder(right)
}
Now, I'll be honest with you: the ++ operator creates a lot of intermediate lists. For a whiteboard interview, this is usually acceptable because it demonstrates you understand the algorithm. However, if the interviewer asks about performance, you'll need to talk about the accumulator pattern (which we'll touch on in a second).
How do I avoid the performance hit of list concatenation?
If you're dealing with a massive tree, ++ will kill your performance. To fix this, we use a helper function with an accumulator. Instead of returning a list and appending to it, we pass the "rest of the list" down into the recursive call. This allows us to use the :: (cons) operator, which is $O(1)$.
def inOrderEfficient(tree: BinaryTree): List[Int] = {
def traverse(t: BinaryTree, acc: List[Int]): List[Int] = t match {
case Empty => acc
case Node(v, left, right) =>
// We go right first, then root, then left,
// because we are building the list from the end backwards.
traverse(left, v :: traverse(right, acc))
}
traverse(tree, Nil)
}
This pattern is a bit harder to wrap your head around at first. You're essentially saying: "Give me the traversal of the right subtree, stick the current value on the front of that, and then use that entire result as the starting point for the left subtree." It's a common functional idiom that saves you from $O(n^2)$ complexity in the worst case.
Wait, isn't this still going to blow the stack on a deep tree?
Yes, it is. In the examples above, the recursive calls aren't in the "tail position," meaning the program has to remember where it was to come back and finish the concatenation. In a production Scala environment, if you expect a tree to be 10,000 nodes deep, you'd either need to use scala.collection.immutable.Stack to simulate the recursion or use a Trampoline.
But here is a tip: on a whiteboard, don't start with the complex stack-safe version. Start with the simple recursive version I showed first. Once it's working, mention the stack overflow risk. It shows you're thinking about edge cases and production stability without making your initial code unreadable.
📋 Practical Task
Exercise: Implement a Post-Order Traversal
Using the BinaryTree sealed trait and case classes defined in the lesson, implement a function called postOrder. Unlike the in-order traversal, a post-order traversal must visit the nodes in this specific sequence: Left Subtree, Right Subtree, and finally the Root Node.
Your function should take a BinaryTree and return a List[Int]. Test your implementation with a tree that has at least three levels of depth to ensure your recursive calls are hitting the leaf nodes before returning the root values.
There are no comments for now.