-
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
163: Common Scala Interview Questions on Pattern Matching Revisited
I remember mentoring a developer a few years back who was interviewing for a senior role at a high-frequency trading firm. He was brilliant with algorithms, but he hit a wall when the interviewer asked him to write a function that handled a List[T] differently depending on whether it contained Strings or Integers. He confidently wrote a match block with case list: List[String] => ... and case list: List[Int] => .... He was baffled when the compiler screamed at him. He spent ten minutes trying to "fix" the syntax, not realizing he was fighting a fundamental JVM limitation: type erasure. It was a humbling moment, but it's exactly why we need to revisit the "interview" side of pattern matching—because the gaps between "it works in a tutorial" and "it works in production" are usually where the hardest questions live.
The Type Erasure Trap
When you're in an interview, if someone asks you to match on a generic collection, your internal alarm should go off. Because of type erasure, the JVM doesn't actually know what's inside a List at runtime. To the JVM, a List[String] and a List[Int] are both just List. If you try to match on the specific generic type, you'll get a compile-time error telling you that the match is "unrefined."
I usually tell people that the "correct" interview answer involves mentioning TypeTag or ClassTag, but the "software engineer" answer is to avoid the pattern entirely. Instead of matching on the list type, match on the elements of the list. For example, matching on case head :: tail and then checking the type of the head is often a safer, more idiomatic way to handle this without fighting the compiler. It's a subtle distinction, but it shows you actually understand how the Scala compiler maps to the bytecode.
Exhaustiveness and the Sealed Trait Guarantee
You've likely used sealed traits before, but in an interview context, the question isn't just "what is it?" but "why does it matter for pattern matching?" The magic here is the compiler's ability to perform exhaustiveness checking. If you have a sealed trait Shape with Circle and Square, and you forget to handle Square in your match block, the compiler gives you a warning.
Now, here is the nuance: if you use a default case (the dreaded case _ =>), you effectively silence that warning. I've seen developers do this as a "safety net" to avoid MatchError, but in a professional codebase, that's often a mistake. By adding a catch-all, you're telling the compiler, "Stop helping me." If you add a Triangle to your Shape trait six months from now, you won't get a single compiler warning telling you where your logic is now incomplete. You'll just get a generic behavior (or a bug) at runtime. I always advocate for removing the catch-all case in sealed hierarchies to let the compiler act as your automated Todo list.
Custom Extraction with the Unapply Method
Most people stop at case classes, but if you want to impress in a technical deep-dive, you need to talk about unapply. This is what allows you to "deconstruct" an object that isn't necessarily a case class. Think of it as the inverse of a constructor. You're defining how an object should be ripped apart for a match block.
case class User(name: String, email: String)
object UserExtractor {
def unapply(s: String): Option[(String, String)] = {
val parts = s.split("@")
if (parts.length == 2) Some((parts(0), parts(1))) else None
}
}
// Now you can match on a raw string as if it were a User object
val input = "dev_scala@example.com"
input match {
case UserExtractor(name, domain) => println(s"User $name is at $domain")
case _ => println("Invalid format")
}
This pattern is incredibly powerful for validation. Instead of writing a bunch of if/else blocks to parse a string, you wrap that logic in an extractor. It keeps your business logic clean and your pattern matches readable. It's the difference between writing code that just "works" and writing code that is actually architected for growth.
📋 Practical Task
Implementing a Validated Social Security Number Extractor
In this exercise, you will implement a custom extractor to handle the validation and parsing of Social Security Numbers (SSNs). Instead of using a case class, you will create an object that uses the unapply method to ensure a string is in the correct format (XXX-XX-XXXX) before allowing it to be matched.
Requirements:
- Create an object named
SSNExtractor. - Implement an
unapplymethod that takes aStringand returns anOption[(String, String, String)]containing the three segments of the SSN. - The
unapplymethod should only returnSomeif the string matches the regex pattern^\d{3}-\d{2}-\d{4}$. - Write a test match block that takes a list of strings (some valid, some invalid) and uses
SSNExtractorto print "Valid SSN: [segments]" for matches and "Invalid Format" for others.
There are no comments for now.