Skip to content
Tutorials Center
My learning

Topics

Lesson 47 of 48 ยท Practice and Review

Elixir Interview Questions

Prepare for an Elixir interview: clear answers on immutability, the BEAM, processes, OTP, pattern matching and tricky code snippets, with examples.

Elixir interviews usually test whether you understand the ideas behind the language, not whether you remember function names. Interviewers want to hear why data is immutable, what a process really is, how supervisors help, and how pattern matching works. This lesson groups common questions by topic and gives short, accurate answers you can say in your own words, plus a set of tricky snippets.

Elixir is not installed on this site, so the Elixir code is read-only. Two JavaScript stand-ins let you play with the ideas of immutability and mailbox matching. Follow the links to the full lessons wherever an answer feels too short.

What interviewers look for

Concepts Immutability, processes, fault tolerance.
Reading code Predict what a snippet prints.
Design Where state lives, who supervises.
Trade-offs When not to use a process or macro.

Explain the why, not only the what.

1 / 2

Language basics

What is Elixir and what does it run on? Elixir is a functional, dynamically typed language that compiles to bytecode for the BEAM, the Erlang virtual machine. It can call any Erlang or OTP library directly. The BEAM was built for telecom systems: massive concurrency, low latency, fault tolerance and hot code upgrades. See What Is Elixir?.

Is Elixir compiled or interpreted? It is compiled to BEAM bytecode (.beam files), either ahead of time with mix compile or in memory when you run an .exs script. The BEAM then runs that bytecode, and on common platforms it also has a JIT compiler.

What are atoms? Atoms are constants whose value is their own name, such as :ok or :error. They are stored once in a global atom table, so comparing them is fast. The table has a fixed limit and atoms are not garbage collected, so never create atoms from untrusted input (use String.to_existing_atom/1 or keep strings). See Security Basics.

What is truthy in Elixir? Only false and nil are falsy. Everything else, including 0, "" and [], is truthy. See Basic Types.

What is the difference between == and ===? == compares values with number coercion, so 1 == 1.0 is true; === is strict and 1 === 1.0 is false. See Operators and Comparison.

What is the difference between and/or and &&/||? and/or require a boolean on the left and raise otherwise; &&/|| accept any value and use truthiness. For example nil || "default" returns "default", but nil or "default" raises a BadBooleanError.

Strings versus charlists? A string such as "hello" is a UTF-8 binary. A charlist such as ~c"hello" is a list of integer code points, which is what older Erlang libraries expect. Use strings in new code. See Strings and Binaries.

Immutability and the match operator

What does immutability mean here? Once a value exists it never changes. Functions that "modify" data return a new value, and the old one remains valid. This removes a whole class of bugs and makes sharing data between processes safe. Persistent data structures share unchanged parts, so a new list or map is usually cheap. See Immutability and Rebinding.

If data is immutable, what does x = 2 after x = 1 do? It rebinds the name x to a new value. The old value 1 is not modified, and any closure that captured the earlier binding still sees 1.

What is the = operator? It is the match operator, not assignment. The left side is a pattern and the right side a value; Elixir tries to make them equal by binding variables. {a, b} = {1, 2} binds both, and {1, b} = {2, 3} raises a MatchError. See Pattern Matching.

What does the pin operator do? ^x matches against the current value of x instead of rebinding it: {^x, y} = {1, 2} only matches if x is 1.

The first editor shows rebinding against mutation using a JavaScript closure. It is a stand-in; in Elixir you do not have a mutation to compare with.

JS Rebinding is not mutation (JavaScript stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JavaScript let can be reassigned in place, whereas Elixir has no assignment of that kind; the closure example is the part that carries over.

The BEAM and processes

What is a process? A process is a very light unit of execution managed by the BEAM, not an operating-system process or thread. It has its own heap, a mailbox, and a garbage collector that runs per process. Creating one takes microseconds and a few kilobytes, so applications run hundreds of thousands. See Processes and Message Passing.

How do processes communicate? Only by asynchronous message passing with send/2; messages are copied (except large binaries, which are shared) into the receiver's mailbox, and receive pulls out the first message matching a pattern. There is no shared memory.

How does the scheduler work? The BEAM runs one scheduler per CPU core. Scheduling is preemptive, based on a budget of function calls ("reductions"), so one busy process cannot starve others, which is why latency stays steady.

What does "let it crash" mean? Instead of defensive code for every possible error, write the happy path, let unexpected failures kill the process, and have a supervisor restart it in a known good state. This works because processes are isolated and cheap. See Links, Monitors and Let It Crash.

Link versus monitor? A link is bidirectional: if one process dies abnormally, the other receives an exit signal and dies too, unless it traps exits. A monitor is one-way: the watcher receives a {:DOWN, ...} message and is not affected.

What happens when a mailbox gets very long? Memory grows, and a receive with a pattern that rarely matches has to scan the whole mailbox every time, which gets slow. Mailboxes have no limit, so apply back-pressure and always include a catch-all clause for messages you do not expect.

The next editor models selective receive. Only the first message that matches the pattern is taken; unmatched messages stay in the mailbox in their original order.

JS Selective receive (JavaScript stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

OTP

What is OTP? The Open Telecom Platform: a set of libraries and design principles (behaviours such as GenServer, Supervisor and Application) for building fault-tolerant, long-running systems.

What is a GenServer? A behaviour module that wraps the loop of a process holding state. You implement callbacks (init/1, handle_call/3, handle_cast/2, handle_info/2) and OTP provides the loop, timeouts, naming and debugging. See GenServer.

call versus cast? GenServer.call/3 is synchronous: it waits for a reply (5 seconds by default) and gives back-pressure. GenServer.cast/2 is asynchronous fire-and-forget and returns :ok at once, without knowing whether the message was handled.

What do supervisors do, and what are the restart strategies? They start, monitor and restart child processes according to a strategy: :one_for_one (restart only the failed child), :one_for_all (restart all) and :rest_for_one (the failed child and those started after it). max_restarts within max_seconds stops endless restart loops. See Supervisors and Supervision Trees.

Agent, Task or GenServer? Use Agent for simple shared state, Task for one piece of concurrent work and GenServer when you need custom messages, timeouts or lifecycle control. See Keeping State in a Process.

When should you not use a process? For code organization. Modules and functions organize code; processes model concurrency, isolation and state that must be serialized. Using a GenServer as a bottleneck for pure computation is a common mistake.

Functions and data

How does the pipe operator work? a |> f(b) becomes f(a, b): the left value is inserted as the first argument. It encourages designing functions that take the main data first. See The Pipe Operator.

Enum versus Stream? Enum is eager and returns a result at once; Stream is lazy and builds a recipe that runs only when an Enum function consumes it, so it handles large or infinite data without building intermediate lists. See Lazy Processing with Stream.

Why is appending to a list slow? Lists are singly linked, so list ++ [x] copies the whole left list. Prepend with [x | list] and reverse once at the end. See Tuples and Lists.

What is a struct? A map with a fixed set of keys and a name, defined with defstruct inside a module. You can pattern match on %User{}, and @enforce_keys makes some fields mandatory. See Maps and Structs.

What is a protocol, and how does it differ from a behaviour? A protocol dispatches on the data type of the first argument (polymorphism, like Enumerable). A behaviour is a contract of callbacks that a module promises to implement (like GenServer). See Protocols and Behaviours.

What are macros and when do you avoid them? Macros run at compile time, receive code as an AST and return new code. Avoid them when a function would do: macros are harder to read, test and debug. See Metaprogramming and Macros.

Tricky snippets

What does each of these do? Think first, then read the answer.

ELIXIR
# 1
[1, 2, 3] -- [2] ++ [3]

Answer: [1]. The operators ++ and -- are right-associative, so this is [1, 2, 3] -- ([2] ++ [3]), which removes both 2 and 3.

ELIXIR
# 2
if nil, do: "yes", else: "no"

Answer: "no", because nil is falsy.

ELIXIR
# 3
%{a: 1} == %{a: 1.0}

Answer: true, since == compares numbers by value; === would give false.

ELIXIR
# 4
{:ok, value} = {:error, :not_found}

Answer: a MatchError. The tuple tags differ, so the pattern does not match.

ELIXIR
# 5
x = 10
f = fn -> x + 1 end
x = 20
f.()

Answer: 11. The anonymous function captured the earlier binding of x.

ELIXIR
# 6
Enum.map(1..3, fn n -> n * 2 end) |> Enum.sum()

Answer: 12.

ELIXIR
# 7
case {1, 2} do
  {a, a} -> "same"
  {_, _} -> "different"
end

Answer: "different". Using the same variable twice in one pattern means both parts must be equal, and 1 is not 2.

ELIXIR
# 8
spawn(fn -> raise "boom" end)
IO.puts("still here")

Answer: The new process crashes and logs an error report, but the spawning process is not linked and keeps running, so still here is printed. With spawn_link/1 the caller would be taken down as well (unless it traps exits).

How to answer well

  • Start with the idea in one sentence, then give a small example.
  • Mention trade-offs: "a GenServer serializes access, which is safe, but also a possible bottleneck".
  • If you do not know something, say how you would find out (h Enum.map in IEx, the docs, a quick test).
  • Practice by writing code: see Practice Exercises with Solutions.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the result of nil || "default" and nil or "default"?

    || works with truthiness, while or requires a boolean on its left side.
  2. 2. What is the difference between a link and a monitor?

    A monitor is a one-way observation and never takes the watcher down.
  3. 3. Which restart strategy restarts the failed child and the children started after it?

    :rest_for_one is designed for children where later ones depend on earlier ones.
  4. 4. What does [1, 2, 3] -- [2] ++ [3] return?

    Both operators are right-associative, so it is [1, 2, 3] -- ([2] ++ [3]).
  5. 5. Why should you avoid String.to_atom/1 on user input?

    An attacker could create atoms until the table is full and the VM crashes.

Summary

  • Know the core ideas: immutability, rebinding, = as match, truthiness (only false and nil are falsy).
  • Describe processes as cheap, isolated, preemptively scheduled units that communicate by messages.
  • Explain OTP: GenServer for state and protocol, supervisors for restarts, "let it crash" as the failure philosophy.
  • Be ready to read snippets with right-associative ++/--, pattern repetition and closures.
  • Back every claim with a small example and a trade-off.

Next up: Elixir Cheat Sheet.