As Scala slowly goes mainstream, demand for programmers familiar with the language is rising. Though it ranked 39th in August’s TIOBE Programming Community Index, enthusiasts predict it will become a leading technology in the enterprise by 2018.
If you’re interviewing for a Scala developer’s job, how should you prepare? Jamie Allen, director of global services for San Francisco-based Typesafe, initially probes candidates for their general understanding of the language, then explores their specific expertise in things such as libraries and case classes. Here are some of the questions he asks:
What’s the difference between the following terms and types in Scala: ‘Nil,’ ‘Null,’ ‘None,’ ‘Nothing’?
- What Most People Say: “Nil is the end of a List. Null is the absence of value. None is the value of an Option if it has no value in it.”
- What You Should Say: “Null is a type that represents the absence of type information for complex types that are inherited from AnyRef. Nothing is the bottom type of the entire Scala type system, incorporating all types under AnyVal and AnyRef. Nothing is commonly used as a return type from a method that does not terminate normally and throws an exception.”
- Why You Should Say: The first answer isn’t bad, Allen says, but the developer didn’t realize that the list only contained terms and didn’t explain the distinction between types and terms. Plus, he didn’t explore the type system of Scala via the given bottom types. Being able to explain these varying—and often confusing—parts of the language shows that you understand fundamental concepts about Scala from the type system to collections to Option containers to the JVM runtime itself. “Even if you don’t use terms and types, they’re bound to pop up when you’re coding, so you need to know what they’re trying to convey,” Allen observes.
Tip: If you want to know more about bottom types, Allen recommends this article by James Iry.
What is ‘Option’ and how is it used?
- What Most People Say: “Option is a wrapper type that avoids the occurrence of a NullPointerException in your code. You can use the get() or map() methods.”
- What You Should Say: “Option is a container that provides the ability to differentiate within the type system those values that can be nulled and those that cannot be. While it is possible to obtain the value inside of an option using get(), it’s preferable to leverage higher-order functions such as map, flatMap and foreach. Here’s how I use them: I leverage map when I want to return another Option of the value. I use flatMap to compose multiple Option usages together, and foreach for operations that produce effects on the value inside the Option without returning a new value and for expressions for composing them together in a clear and concise way.”
- Why You Should Say It: The second answer demonstrates an understanding of several comprehensive concepts. For instance, it explains how the type system provides semantic value as well as compile-time enforcement of what gets done and when. It also shows that you have a basic grasp of functional programming, such as how to use lambdas or named functions with higher order functions. Moreover, it conveys that you’re thinking about functional composition.
Explain the difference between ‘concurrency’ and ‘parallelism,’ and name some constructs you can use in Scala to leverage both.
- What Most People Say: “Concurrency is about avoiding access to mutable state by multiple threads at the same time. Parallelism is taking a single task and breaking it apart to be performed by multiple threads at the same time. Actors are about concurrency, parallel collections are about parallelism.”
- What You Should Say: “Concurrency is when several computations are executing sequentially during overlapping time periods, while parallelism describes processes that are executed simultaneously. The concepts are often confused because actors can be concurrent and parallel, sometimes simultaneously. For example, Node.js has concurrency via its event loop despite being a single threaded implementation. Parallel collections are a canonical example of parallelism, but Futures and the Async library can be as well. I benchmark the performance of the parallelism construct to verify that the cost of parallelizing and joining a task isn’t greater than the speed of executing in parallel, as compared to a tight loop on a single thread of execution over an array-based data structure, as defined by Amdahl’s Law.”
- Why You Should Say It: Again, the first answer isn’t bad; however, you can have concurrency without multiple physical threads, Allen notes. The second answer shows that you understand the impact of concurrency and parallelism in writing reactive applications that leverage all of the cores on all of the sockets of every machine. “With an increasing number of cores at our disposal, it is important that developers understand how to use them most effectively and the challenges they might face in doing so,” Allen says.
Bonus Question: What is ‘Unit’ and ‘()’?
- What You Should Say: “Unit is a type representing the Scala equivalent of the Java void, while still providing the language with an abstraction over the Java platform. The empty tuple () is a term representing a Unit value.”