OCaml Module 1
Start here: OCaml, opam, dune and the toplevel interview questions
Interview questions on “Start here: OCaml, opam, dune and the toplevel”: warm-ups, then what each harder question tests and how a good answer starts.
Warm-up questions
Short questions with short answers, each answered in full.
-
Fresher to mid
How do ocamlc and ocamlopt differ?
-
Fresher to mid
What is an opam switch, and when would you use a local one?
Answered in the lesson Install OCaml 5.5.1 with opam and use the REPL
-
Fresher to mid
What do dune build, dune exec and dune test do?
Answered in the lesson Your first dune project and editor setup
Questions with model answers
What each question tests, the first step of a good answer and the follow-ups to expect.
-
Mid to senior
A function that sums payment amounts in cents gives correct totals in your native build but wrong, negative totals in the same code compiled with js_of_ocaml for a web page. What is going on, and how would you fix it?
- What it tests
- Knowing that int width depends on the back end (63 bits native, 32 under js_of_ocaml, 31 under wasm_of_ocaml) and choosing a representation that is correct everywhere.
- First step
- Print Sys.int_size and max_int in both builds: 63 bits natively, 32 under js_of_ocaml, so a total above 2,147,483,647 cents, about 21.5 million dollars, wraps around to a negative number in the browser build.
- Follow-ups
-
- Which type would you use for money instead, and what does it cost?
- How would you catch this in tests before it ships?
- Does wasm_of_ocaml make it better or worse?
From the lesson What OCaml is and where it is used
Model answer, Pro. Model answers open with any pass; prices are in the notice on this page.Opening the model answers with your pass…
Model answers open with any MySmartCoPilot pass.
- Pro pass · 1 week₹79$2.99
- 30 days₹179$5.99
- 1 year₹1,699$45
One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.
Passes can’t be bought right now, so the model answers can’t be opened at the moment. If you have a pass, enter your licence key.
It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).
In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 2 minutes
-
Senior to staff
Your team must build a command-line tool that parses and checks large configuration files, and someone suggests OCaml. What would you weigh before agreeing, and how would you test the choice cheaply?
- What it tests
- Judging a language choice by its strengths (static types with inference, pattern matching, native speed, a stable toolchain) against team skills, libraries and hiring, with a small time-boxed trial.
- First step
- Name what the tool needs: correct parsing and checking, speed on large files, one binary to ship. Then match OCaml's strengths to those needs and list the risks that are about the team rather than the language.
- Follow-ups
-
- What would you build in the trial, and how long would it run?
- Which libraries would you check exist before committing?
- When would you choose a different language?
From the lesson What OCaml is and where it is used
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 2 minutes
-
Mid to senior
A project builds on your laptop, but the continuous integration job fails with an error about a library version you never installed there. You suspect the job uses a different compiler or switch. How do you find out and fix it?
- What it tests
- Diagnosing which switch and compiler a command really uses (opam switch show, ocaml -version, opam exec) and making builds name their switch or use a local one with locked dependencies.
- First step
- Make the job print what it uses before it builds: opam switch show, ocaml -version, which dune and the version of the library in question. Compare that with the same lines on your laptop.
- Follow-ups
-
- The job runs eval $(opam env) in one step and builds in the next. Why can that fail?
- How would a local switch help here?
- How do you make sure everyone gets the same library versions?
From the lesson Install OCaml 5.5.1 with opam and use the REPL
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 2 minutes
-
Mid to senior
A program that uses Unix.pipe crashes with a segmentation fault on one developer's Mac but runs on everyone else's, all with OCaml 5.5.1. How would you narrow it down without guessing?
- What it tests
- Separating code bugs from build and platform causes, reproducing with a minimal program, and using upstream issues and build settings (here the macOS SDK) to find a known trap.
- First step
- Reduce it: build the four-line program that only opens and closes a pipe on the failing Mac. If that crashes too, the cause is the build of OCaml on that machine, not the application's code.
- Follow-ups
-
- The minimal program crashes. What do you compare between the two Macs?
- How do you confirm it is the known upstream issue?
- What do you do until the fixed release is out?
From the lesson Install OCaml 5.5.1 with opam and use the REPL
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 2 minutes
-
Fresher to senior
Your dune project builds on your laptop, but a teammate's clone of the same commit stops with "Error (warning 26 [unused-var])" and another with "Unbound module Alcotest". Explain each failure and how you would make the project build the same everywhere.
- What it tests
- Knowing dune's profiles (warnings as errors in dev), how stanzas list libraries, and how a project declares its dependencies so every machine installs the same ones.
- First step
- Treat them as two different problems. The warning fails only in the dev profile, so ask which profile each machine uses; the unbound module means the library is missing from the switch or from the stanza, so check both.
- Follow-ups
-
- Why might your laptop not show the warning at all?
- Where should the project declare that it needs Alcotest?
- What would you add to the repository so a new teammate's first build works?
From the lesson Your first dune project and editor setup
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
Code reviews in your OCaml team keep arguing about layout, and two developers' editors reformat each other's code. How would you set up formatting so the arguments stop?
- What it tests
- Using ocamlformat through dune fmt with a committed .ocamlformat that pins the version, and checking formatting in CI with dune fmt's exit status.
- First step
- Make one tool and one version decide: commit a .ocamlformat with version = 0.29.0 at the project root, install that ocamlformat version for everyone, and run dune fmt once in its own commit.
- Follow-ups
-
- A developer has ocamlformat 0.28.1 installed. What happens and what do they do?
- How does CI catch unformatted code?
- Why put the first big reformat in a commit of its own?
From the lesson Your first dune project and editor setup
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 2 minutes