Skip to content

Mojo Interview Questions

These are the questions you’re likely to hear if a job or a code review touches Mojo, from “what is it?” to ownership and compile-time code. Each answer is short enough to say out loud, and each one is correct for Mojo 1.2. Many older answers you’ll find online were true for 2023 to 2025 Mojo and aren’t any more, so where something changed, the answer says so.

Try answering each question yourself before you read the answer. If one stumps you, the link at the end of the answer goes to the lesson that explains it.

A programming language made by Modular. It looks a lot like Python, but it’s compiled and statically typed, with ownership rules and compile-time metaprogramming, so it can be used for systems programming and for CPUs and GPUs. Version 1.0 came out in August 2026, and the compiler has been open source (Apache 2.0 with LLVM exceptions) since then. More in Mojo Intro.

No. Mojo borrows Python’s syntax, but a Python file isn’t a valid Mojo program: you need var for new names, types on function arguments, code inside functions, and structs instead of classes. The official roadmap says Mojo “may or may not evolve into a full superset of Python”. What Mojo can do is call Python libraries and be called from Python. See Mojo vs Python.

It installs like a Python package. With uv: uv init app && cd app && uv add mojo, then uv run mojo run hello.mojo. mojo build hello.mojo -o hello makes an executable. It runs on Linux and macOS (Apple silicon); on Windows you use WSL. See Mojo Get Started.

4. What’s the difference between def and fn?

Section titled “4. What’s the difference between def and fn?”

In Mojo 1.2 there is no fn: it was deprecated in 1.0 and removed in 1.1. def is now the only way to declare a function, and it has the rules fn used to have: argument types are checked, and a def does not raise unless you mark it raises. Old answers that say “def is the dynamic, Python-like one” are out of date.

5. How do you declare variables and constants?

Section titled “5. How do you declare variables and constants?”

var creates a new variable, and comptime creates a compile-time constant. let and alias are gone.

comptime MAX_USERS = 100
def main():
var count = 3
var price: Float64 = 9.5
count += 1
print(count, price, MAX_USERS)

Output:

4 9.5 100

Assigning to a name that doesn’t exist yet, like total = 5, is a compile error (“implicit declaration of ‘total’ is not allowed”). See Mojo Variables and Mojo Constants.

Because “the length of a string” is ambiguous once you leave plain ASCII. Mojo makes you say which one you mean: s.byte_length() for bytes, s.count_codepoints() for Unicode code points, s.count_graphemes() for what a user sees as characters. Indexing works the same way: s[byte=0], not s[0]. See Mojo Strings in Depth.

With two Int variables, / stays an Int and truncates toward zero. With literals it’s done exactly at compile time, so 7 / 2 is 3.5. // always floors.

def main():
var a = 7
var b = 2
print(7 / 2, a / b, a // b)
print(Float64(a) / Float64(b))

Output:

3.5 3 3
3.5

This surprises Python developers, where / always gives a float. Convert to Float64 when you want a fractional result. See Mojo Numbers.

It wraps around silently, like C or Rust in release mode. There’s no error and no switch to big integers like Python.

def main():
var big = 9223372036854775807
big += 1
print(big)

Output:

-9223372036854775808

See Mojo Integer Overflow.

9. What’s the difference between List and Array?

Section titled “9. What’s the difference between List and Array?”

List[T] grows and shrinks at run time and lives on the heap. Array[T, n] has a size fixed at compile time. A bare literal like [1, 2, 3] makes an Array, so you write var xs: List[Int] = [1, 2, 3] when you want a list. See Mojo Lists and Mojo Arrays.

With the std.testing module: write test_... functions that use assert_equal and friends, and run them with TestSuite from main. There’s no mojo test command; you run the test file with mojo run. See Mojo Testing.

11. How is a struct different from a Python class?

Section titled “11. How is a struct different from a Python class?”

A struct’s fields are declared up front with fixed types, so its layout is known when the program is built. You can’t add attributes at run time, there’s no inheritance, and structs are values: assigning one doesn’t create a second name for the same object. Shared behavior comes from traits instead. See Mojo Structs.

A set of methods a type promises to have. Generic code can then accept “any type with these methods”. It’s similar to an interface in Java or a trait in Rust.

trait Speaker:
def speak(self) -> String:
...
@fieldwise_init
struct Dog(Speaker):
def speak(self) -> String:
return "Woof"
@fieldwise_init
struct Robot(Speaker):
def speak(self) -> String:
return "Beep"
def greet[T: Speaker](x: T):
print(x.speak())
def main():
greet(Dog())
greet(Robot())

Output:

Woof
Beep

Mojo builds a separate version of greet for each type, so there’s no run-time lookup. See Mojo Traits.

It writes an __init__ that takes one argument per field, in order. It replaced part of what the old @value decorator did; the rest (copying, printing) now comes from traits you list, like Copyable and Writable.

Conform to the Writable trait. If you don’t write anything else, print shows the struct name and its fields, like Point(x=1, y=2). For your own format, add def write_to(self, mut writer: Some[Writer]). Stringable and __str__ no longer exist. See Mojo Printing Your Types.

15. What’s the difference between Copyable and ImplicitlyCopyable?

Section titled “15. What’s the difference between Copyable and ImplicitlyCopyable?”

Copyable lets you copy a value on purpose with .copy(). ImplicitlyCopyable also lets plain assignment copy it, like a number. Most types, including List and Dict, are only Copyable, so Mojo never copies a big value behind your back:

@fieldwise_init
struct Point(Copyable):
var x: Int
def main():
var a = Point(1)
var b = a
print(b.x)
points.mojo:8:13: error: value of type 'Point' cannot be implicitly copied, it does not conform to 'ImplicitlyCopyable'
var b = a
^
points.mojo:8:13: note: consider transferring the value with '^'
var b = a
^
^
points.mojo:8:13: note: you can copy it explicitly with '.copy()'
var b = a
^
.copy()
mojo: error: failed to parse the provided Mojo source module

Write var b = a.copy() for a copy, or var b = a^ to move it. See Mojo Common Traits.

Keywords that say what a function may do with an argument:

Convention Meaning
(none) Read-only reference: no copy, can’t change it
mut Mutable reference: changes are seen by the caller
var The function owns its own value (a copy, or a moved value)
ref A reference that keeps the caller’s mutability
out The function sets up an uninitialized value (used by __init__)
deinit The value is being destroyed (used by __deinit__ and moves)

Old names: inout became mut, owned became var, borrowed became the default. See Mojo Argument Conventions.

It moves a value: ownership passes to the new place, and the old name can’t be used afterwards. Use it to hand over something big without copying, and when returning a local List or Dict (return xs^). Using a moved value is a compile error: “use of uninitialized value”. See Mojo Moving Values.

Right after its last use, not at the end of the scope like C++ and not “sometime later” like a garbage collector. There’s no garbage collector and no reference counting for ordinary values. A struct can run clean-up code in def __deinit__(deinit self). See Mojo Value Lifecycle.

The compiler’s record of which value a reference points into. It lets Mojo reject references that would dangle. For example, holding a reference to a list item while the list grows:

def main():
var xs: List[Int] = [1, 2, 3]
ref first = xs[0]
xs.append(4)
print(first)
refs.mojo:5:11: error: use of invalidated interior reference 'xs["element"]'
print(first)
^
refs.mojo:4:14: note: origin was invalidated here
xs.append(4)
^
mojo: error: failed to run the pass manager

append may move the list’s items to new memory, so first could point at freed memory. See Mojo References & Origins.

Yes. Pointer is the raw pointer type (in 1.0 UnsafePointer was merged into it and is now a deprecated name), and unsafe operations are marked on the operation, like unsafe_load, rather than on the type. For owned heap values there are smart pointers such as OwnedPointer and ArcPointer in std.memory. Most code never needs pointers; List, Span and references cover the usual cases. See Mojo Pointers.

A function that can fail is marked raises; callers either mark themselves raises too or handle the error with try/except. A non-raising function can’t call a raising one without handling it: that’s a compile error. The manual’s errors page explains that errors are values, returned as an alternative result, not stack-unwinding exceptions like in C++ or Java. See Mojo Errors.

Yes. Put the type in the signature: def withdraw(mut self, amount: Int) raises InsufficientFunds:. Inside except e:, e has that type, so you can read its fields. Each try has a single except, and main can only raise the plain Error type. See Mojo Custom Errors.

23. What’s the difference between a parameter and an argument?

Section titled “23. What’s the difference between a parameter and an argument?”

Parameters go in square brackets and are known when the program is built. Arguments go in round brackets and are known when it runs.

def repeat[times: Int](msg: String):
comptime for _ in range(times):
print(msg, end=" ")
print()
def main():
repeat[3]("hi")

Output:

hi hi hi

times is a parameter, so comptime for can unroll the loop. msg is an ordinary argument. See Mojo Parameters.

24. What are comptime if and comptime for?

Section titled “24. What are comptime if and comptime for?”

An if or for that runs while the program is built. Only the chosen branch, or the unrolled loop body, ends up in the program. They replaced @parameter if and @parameter for, which were removed in 1.1. See Mojo Compile-Time If & Loops.

25. How do you restrict which parameter values are allowed?

Section titled “25. How do you restrict which parameter values are allowed?”

With a where clause on the declaration, like def batches[size: Int](total: Int) -> Int where size > 0:. A call that breaks it fails to compile, at the call site. Inside a function you can also use comptime assert with your own message. See Mojo Constraints.

A built-in type for a small vector of numbers that the CPU can process in one instruction: SIMD[DType.float32, 4]. Scalars are just SIMD vectors of width 1, and Int itself is an alias for Scalar[DType.int]. See Mojo SIMD.

For typed, compiled code such as numeric loops, it usually is, because there’s no interpreter and the compiler can use SIMD and multiple cores. But it depends on the code: calling into Python from Mojo runs at Python speed, and a library like NumPy is already fast. The honest answer is “measure it”: see Mojo Benchmarking and Project: Speed Up a Python Script.

28. How do you use a Python library from Mojo?

Section titled “28. How do you use a Python library from Mojo?”

Import it with Python.import_module. Values from Python are PythonObjects; convert them back with Int(py=...), Float64(py=...) or String(py=...). Calls into Python can raise, so the function needs raises.

from std.python import Python
def main() raises:
var math = Python.import_module("math")
var r = math.sqrt(81)
print(Float64(py=r) + 1)

Output:

10.0

Mojo uses the Python it finds first on your PATH (the project’s .venv under uv run). See Mojo Calling Python.

Yes. You write a Mojo module that builds a Python module with PythonModuleBuilder and exports an init function, and on the Python side import mojo.importer lets import my_module compile and load the .mojo file. It’s a good way to speed up the slow part of a Python program. See Mojo Calling Mojo from Python.

You write a kernel as a normal Mojo function and launch it over many threads through a device context. Since 1.0 the GPU APIs live in Modular’s separate max package (uv add max), not the standard library. One detail interviewers like: Int can’t be passed to a GPU kernel in 1.x, because its width depends on the platform; use a fixed-width type like Int32. See Mojo GPU Basics.

  • def is the only function keyword; var and comptime replace let and alias.
  • Mojo isn’t a Python superset: types, structs and traits, no classes or inheritance.
  • Copyable means .copy(), ImplicitlyCopyable means plain assignment copies too.
  • Arguments are read-only by default; mut, var, ref, out, deinit change that, and ^ moves.
  • Values are destroyed right after their last use; origins stop dangling references.
  • Parameters are compile-time, arguments are run-time; where clauses check parameters while building.

Next up: Mojo Roadmap, what Modular is planning and what’s changed in the latest releases.

Quick check

1. An interviewer says "def functions in Mojo are dynamic, and fn functions are strict." What is true for Mojo 1.2?
2. What does var b = a do when a is a List[Int]?
3. When is a value destroyed in Mojo?