Erlang/Elixir Interview Questions
Explain tail-call optimisation
When Beam(Erlang VM) sees a function calling itself as the last executable statement in a function, it simply jumps back to the start of the function. If the recursive call has arguments then these replace the original parameters.
Explain the crucial feature of Erlang/Elixir
The idea is to pack code into manageable portions executed independently and concurrently.
spawn vs spawn_link
spawn is a function provided by erlang which takes a single function as an argument and runs it.
spwan_link is similar to spawn except it creates a process and links it and all this is executed as an atomic operation.
Process.flag(:trap_exit, true) — explain
This converts exit signals from a linked process into a message by trapping the exit.
Monitor vs links
- link is bidirectional i.e if any process dies the other is notified and vice versa. While monitors are uni-directional i.e lets a process spawn another process and be notified of its termination, not the other way round.
- links cannot be stacked while monitors can.
- links are used when one process cannot survive without the other. Monitors are needed to know when the other process exits and you want to be notified about it.
Define a node
It is simply Erlang VM called the Beam. Beam in itself is a little OS running on top of your OS.
OTP is a package that includes Erlang, a database(Mnesia) and an innumerable number of libraries like GenServer, GenEvent etc.
Reduction is a measure of the amount of work the server does. It is used in process scheduling as a way of making sure all processes get a fair share of the available CPU.
Define OTP GenServer
An OTP GenServer is just a regular Elixir process in which the message handling is abstracted out.
It is a process that uses the OTP supervisor behaviour. It is given a list of processes to monitor and is told what to do if a process dies and how to prevent restart loops.