-
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
41: Common Scala Interview Questions on Pattern Matching
A few years ago, I was interviewing a candidate for a senior Scala role. He was technically brilliant and breezed through the algorithmic challenges, but we hit a wall when I asked him how the compiler actually "knows" when a pattern match is exhaustive. He gave a vague answer about "Scala magic," and when I pressed him on the role of the sealed keyword, he stumbled. It was a classic case of knowing how to use the syntax without understanding the mechanics under the hood. In a high-stakes interview, that's the difference between looking like a developer who learned a few tutorials and someone who truly understands the type system.
The Exhaustiveness Trap and Sealed Traits
One of the most common interview questions involves why we use sealed traits. If you're asked this, don't just say "it limits inheritance." The real answer is about the compiler's ability to perform exhaustiveness checking. When you mark a trait as sealed, you're telling the Scala compiler that all implementations of this trait are defined in the current file. This allows the compiler to look at your match block and warn you—at compile time—if you've forgotten a case.
sealed trait PaymentMethod
case class CreditCard(number: String, cvv: String) extends PaymentMethod
case class PayPal(email: String) extends PaymentMethod
case class Crypto(walletAddress: String) extends PaymentMethod
def processPayment(method: PaymentMethod): String = method match {
case CreditCard(n, _) => s"Charging card $n"
case PayPal(e) => s"Redirecting to $e"
// If I omit Crypto here, the compiler gives a warning:
// "match may not be exhaustive. It would fail on the following input: Crypto(_)"
}
In an interview, I'd recommend mentioning that this turns a potential runtime MatchError into a compile-time warning. That's the "senior" way to frame the answer: you're talking about safety and reliability, not just syntax.
Demystifying the unapply Method
Interviewers love to ask how case classes actually work during pattern matching. They want to hear the word unapply. Most developers think pattern matching is some built-in language magic, but it's actually just a call to an unapply method. When you write case CreditCard(number, cvv), Scala is actually calling CreditCard.unapply(method), which returns an Option containing the extracted values.
I've seen candidates get tripped up when asked if you can implement pattern matching for a class that isn't a case class. The answer is yes—you just have to write your own extractor object. This is a great way to show off your deeper knowledge. You can create an object with an unapply method to "deconstruct" a regular class or even a string, effectively creating your own custom matching logic that hides the internal complexity of the object being matched.
Guards and the Order of Evaluation
Finally, be prepared to discuss "guards"—those if expressions inside a case statement. A common pitfall is forgetting that patterns are evaluated from top to bottom. If you have a general case above a specific case with a guard, the general case will always win, and the guarded case will be dead code.
val age = 25
age match {
case x if x > 18 => "Adult"
case 25 => "Exactly twenty-five" // This will NEVER be reached
}
I usually tell my mentees to treat guards as a last resort. If you can represent your logic using a sealed hierarchy (like the PaymentMethod example), do that instead. It's more type-safe and allows the compiler to help you. Guards are powerful, but they are essentially "invisible" to the exhaustiveness checker, meaning they can sneak MatchErrors back into your production code if you aren't careful.
📋 Practical Task
Building a Custom Extractor for a Legacy Payment String
In this exercise, you will simulate a real-world scenario where you have to handle "legacy" data. You have a system that receives payment identifiers as strings in the format "TYPE:ID" (e.g., "CC:12345" or "PP:user@email.com"). Instead of manually splitting these strings everywhere in your code, you are tasked with creating a custom extractor object.
Requirements:
- Create an object named
PaymentExtractor. - Implement an
unapplymethod inPaymentExtractorthat takes aStringand returns anOption[(String, String)]. - The
unapplymethod should split the input string by the colon (:). If the string does not contain a colon, it should returnNone. - Write a test function that uses a
matchblock to handle three cases:- When the string matches
PaymentExtractor("CC", id), print"Processing Credit Card with ID: [id]". - When the string matches
PaymentExtractor("PP", email), print"Processing PayPal with Email: [email]". - A fallback case for any other format, printing
"Invalid Payment Format".
- When the string matches
There are no comments for now.