Skip to content

Zig Interview Questions

Zig jobs are still rare, but Zig questions come up more and more: in interviews for systems roles, in code reviews, and when a team asks “should we try Zig for this?”. This page has 30 questions with short answers, the way you’d give them out loud. Where code helps, there’s a small program that was compiled and run on Zig 0.17.1.

Use it to check yourself after the course. Cover the answer, say yours, then compare. If you can explain why, not just what, you know it. Each answer links to the lesson that covers it in more depth.

A general-purpose systems language that aims to be a better C: manual memory management, no garbage collector, and direct C interop, but with safety checks, error handling built into the type system, and compile-time code instead of macros. It also ships a C/C++ compiler and a build system in one download. See Zig Intro.

2. What does “no hidden control flow” mean?

Section titled “2. What does “no hidden control flow” mean?”

If a line of Zig doesn’t look like it calls a function, it doesn’t. There’s no operator overloading, no exceptions, no constructors or destructors running behind your back, and no hidden memory allocations. The official overview lists it next to “no preprocessor, and no macros”. In practice it means you can read a function and know what it does.

3. What’s the difference between const and var?

Section titled “3. What’s the difference between const and var?”

const can’t change after it’s set; var can. Zig is strict: a var that’s never changed is a compile error (local variable is never mutated), and so is any unused local. The idea is that every name in the code means something. See Zig Variables.

The literal 5 has the type comptime_int, which only exists at compile time and has no fixed size. A var needs a real runtime type, so you write var x: i32 = 5;. A const x = 5; is fine, because it can stay a compile-time value.

A way to say “I’ll set this later”. Reading a value before you set it is a bug. In debug builds, Zig fills undefined memory with 0xaa bytes so the bug is easier to spot, but you must never rely on that. See Zig Variables.

There’s no string type. A string is a slice of bytes, []const u8, usually UTF-8. A string literal is a pointer to a constant array with a 0 byte at the end, which coerces to a slice:

const std = @import("std");
pub fn main() void {
const literal = "hi";
const slice: []const u8 = literal;
std.debug.print("{s}\n", .{@typeName(@TypeOf(literal))});
std.debug.print("{s}\n", .{@typeName(@TypeOf(slice))});
std.debug.print("len {d}, byte after the end: {d}\n", .{ literal.len, literal[literal.len] });
}

Output:

*const [2:0]u8
[]const u8
len 2, byte after the end: 0

[2:0]u8 means “2 bytes, with a 0 after them”. The 0 is there for C, but it isn’t counted in len. Compare strings with std.mem.eql(u8, a, b), not ==. See Zig Strings.

7. Arrays, slices and many-item pointers: what’s the difference?

Section titled “7. Arrays, slices and many-item pointers: what’s the difference?”

An array ([5]u8) has its length in its type and is copied by value. A slice ([]u8) is a pointer plus a length, known at run time, with bounds checks. A many-item pointer ([*]u8) is just an address with no length, like a C pointer. Use slices almost everywhere. See Zig Slices.

It looks like a swap, but it isn’t one. The right side isn’t copied first, so a gets b’s value and then b gets the new a:

const std = @import("std");
pub fn main() void {
var a: i32 = 1;
var b: i32 = 2;
a, b = .{ b, a };
std.debug.print("a={d} b={d}\n", .{ a, b });
var c: i32 = 1;
var d: i32 = 2;
std.mem.swap(i32, &c, &d);
std.debug.print("c={d} d={d}\n", .{ c, d });
}

Output:

a=2 b=2
c=2 d=1

Use std.mem.swap. This is a good question because it tests whether someone checks things instead of assuming they work like Python.

A type like !u32 or FileError!u32: either a value or an error from an error set. Errors are plain values, not exceptions. The compiler makes you deal with them: ignoring an error union is a compile error. See Zig Errors.

10. What’s the difference between try and catch?

Section titled “10. What’s the difference between try and catch?”

try x means “if x is an error, return it from this function; otherwise give me the value”. x catch y means “if x is an error, use y instead” (or run a block, with catch |err| ...). try x is short for x catch |err| return err. See Zig Try & Catch.

defer runs when the current block ends, no matter how. errdefer runs only if the function returns an error from that point on. Both run in reverse order:

const std = @import("std");
fn open(name: []const u8) !void {
std.debug.print("open {s}\n", .{name});
defer std.debug.print("defer {s}\n", .{name});
errdefer std.debug.print("errdefer {s}\n", .{name});
if (std.mem.eql(u8, name, "bad")) return error.Broken;
}
pub fn main() void {
open("good") catch {};
open("bad") catch |err| std.debug.print("caught {t}\n", .{err});
}

Output:

open good
defer good
open bad
errdefer bad
defer bad
caught Broken

The classic use: allocate, then errdefer free right away, so a later failure in the same function doesn’t leak. See Zig Errdefer.

12. When do you use an optional instead of an error?

Section titled “12. When do you use an optional instead of an error?”

An optional (?T) means “there may be no value, and that’s normal”, like a search that finds nothing. An error means “something went wrong”, and it carries a name that says what. orelse and if (x) |v| open optionals; try and catch handle errors. See Zig Optionals.

Sometimes. An optional pointer uses the address 0 for null, so it’s the same size as a pointer. Other optionals need an extra flag:

const std = @import("std");
pub fn main() void {
std.debug.print("{d} {d}\n", .{ @sizeOf(?*u8), @sizeOf(*u8) });
std.debug.print("{d} {d}\n", .{ @sizeOf(?u32), @sizeOf(u32) });
}

Output:

8 8
8 4

That’s on a 64-bit machine. The ?u32 grows to 8 bytes because of the flag plus alignment.

When an error travels up through several trys and nobody handles it, a debug build prints the path it took: every place it was returned from. It’s not a stack trace of where the program is now, but of where the error has been. Other build modes print just the error name by default. See Zig Error Return Traces.

15. Why doesn’t Zig have a default allocator?

Section titled “15. Why doesn’t Zig have a default allocator?”

So that allocation is never hidden. Any function that needs heap memory takes a std.mem.Allocator parameter. That tells you at a glance which functions allocate, and lets the caller choose the strategy: an arena, a fixed buffer, a leak-checking allocator in tests. See Zig Allocators.

16. Which allocator would you pick, and when?

Section titled “16. Which allocator would you pick, and when?”
  • init.gpa from main: a good default. In debug and safe builds it’s std.heap.SafeAllocator, which catches leaks and double frees.
  • std.heap.ArenaAllocator: many small allocations that all die together, like one web request. Free everything at once.
  • std.heap.FixedBufferAllocator: a buffer you already have, no heap at all.
  • std.testing.allocator: in tests, so leaks fail the test.

See Zig Choosing an Allocator.

Use an allocator that checks. init.gpa reports leaks with the line that allocated, when the program ends in a debug build. In tests, std.testing.allocator fails the test. std.testing.FailingAllocator makes allocations fail on purpose, to test your cleanup paths. See Zig Memory Leaks.

18. What happens when an integer overflows?

Section titled “18. What happens when an integer overflows?”

In debug and safe builds, the program panics with integer overflow. In fast and small builds, the check is off and overflow is illegal behavior, so anything can happen. If you want wrapping or clamping, say so:

const std = @import("std");
pub fn main() void {
const a: u8 = 250;
std.debug.print("wrapping: {d}\n", .{a +% 10});
std.debug.print("saturating: {d}\n", .{a +| 10});
const r = @addWithOverflow(a, 10);
std.debug.print("checked: {d}, overflowed: {d}\n", .{ r[0], r[1] });
}

Output:

wrapping: 4
saturating: 255
checked: 4, overflowed: 1

See Zig Integer Overflow.

Things your program must never do: overflow, out-of-bounds indexing, unwrapping null, reaching unreachable. Zig checks most of them in debug and safe builds and panics with a clear message. In fast and small builds the checks are gone, like undefined behavior in C. That’s why you test in debug and ship in safe when correctness matters more than the last bit of speed. See Zig Illegal Behavior & Panics.

Code that runs while the compiler works. The same Zig you write for run time can run at compile time: loops, function calls, even building types. There’s no separate macro language:

const std = @import("std");
fn fib(n: u32) u32 {
return if (n < 2) n else fib(n - 1) + fib(n - 2);
}
pub fn main() void {
const at_compile_time = comptime fib(10);
std.debug.print("{d}\n", .{at_compile_time});
}

Output:

55

The program just contains the number 55. The compiler limits how much work it will do (try fib(20) and it asks you to raise the limit with @setEvalBranchQuota). See Zig Comptime.

21. How do generics work without a generics feature?

Section titled “21. How do generics work without a generics feature?”

Types are values at compile time. A function can take comptime T: type, and a function can return a type, which is how generic data structures are made:

const std = @import("std");
fn Pair(comptime T: type) type {
return struct { first: T, second: T };
}
pub fn main() void {
const p: Pair(u8) = .{ .first = 1, .second = 2 };
std.debug.print("{d} {d}\n", .{ p.first, p.second });
}

Output:

1 2

std.ArrayList(T) is made exactly this way. See Zig Generic Data Structures.

Both make a generic function. comptime T: type makes the caller name the type and lets you write it in the signature. anytype infers it from the argument, which is shorter but hides what’s accepted. Prefer the explicit type for public APIs; anytype is common for things like writers. See Zig Generic Functions.

23. Zig has no traits or interfaces. How do you do polymorphism?

Section titled “23. Zig has no traits or interfaces. How do you do polymorphism?”

Three common ways:

  • Tagged unions with a switch, when you know all the cases.
  • anytype or comptime T, checked when the code is compiled (“duck typing” at compile time).
  • A vtable: a struct holding a pointer to the object and a pointer to a table of functions. std.mem.Allocator and std.Io work this way.

See Zig Interfaces.

24. How is a tagged union like a Rust enum?

Section titled “24. How is a tagged union like a Rust enum?”

A union(enum) holds one of several kinds of value and remembers which one. switch must handle every kind, and can capture the value inside:

const std = @import("std");
const Token = union(enum) {
number: i64,
plus,
word: []const u8,
};
pub fn main() void {
const tokens = [_]Token{ .{ .number = 4 }, .plus, .{ .word = "x" } };
for (tokens) |t| {
switch (t) {
.number => |n| std.debug.print("number {d}\n", .{n}),
.plus => std.debug.print("plus\n", .{}),
.word => |w| std.debug.print("word {s}\n", .{w}),
}
}
}

Output:

number 4
plus
word x

See Zig Tagged Unions.

An extern struct uses the C layout, so you can pass it to C code. A packed struct puts fields right next to each other, bit by bit, with no padding: good for flags and hardware registers. A normal struct has no guaranteed layout at all; the compiler may reorder fields. See Zig Packed & Extern Structs.

It compiles and runs build.zig, a Zig program that describes your build: which executables and libraries to make, their dependencies, tests, and custom steps. No Makefile or CMake needed. Dependencies are listed in build.zig.zon. See Zig Build System.

Pass a target: zig build-exe main.zig -target x86_64-windows or zig build -Dtarget=aarch64-linux. Zig ships the C library files it needs for many targets (musl, glibc, MinGW-w64 and others), so there’s usually nothing else to install. See Zig Cross-Compilation.

Zig can call C functions declared with extern, export Zig functions to C with export, and use C types like c_int. zig cc is a drop-in C compiler. For C headers, Zig 0.17 removed the @cImport builtin; you translate headers with the official translate-c package from the build system. See Zig and C.

29. Why do so many functions take an io parameter now?

Section titled “29. Why do so many functions take an io parameter now?”

Since Zig 0.16, I/O goes through the std.Io interface, like memory goes through an allocator. Files, networking, sleeping and io.async all take an io, so the caller decides how I/O happens (blocking threads, or another implementation). main gets one from init.io. See Zig Io Interface.

It depends on the team. Zig isn’t 1.0 yet, so every release breaks some code, and the standard library changes the most. Projects like Ghostty and TigerBeetle use it in production anyway, by pinning one Zig version and upgrading on purpose. An honest answer mentions both the benefits and that upgrade cost. See Zig in the Real World and Zig Upgrading.

  • Language: no hidden control flow, strict const/var and unused-name rules, strings are []const u8, and a, b = .{ b, a } doesn’t swap.
  • Errors: error unions with try/catch, defer and errdefer, optionals for “no value”.
  • Memory: no default allocator; you pass one in, pick it per job, and check for leaks with init.gpa and std.testing.allocator.
  • Comptime: types are values, generics are functions, polymorphism is tagged unions, anytype or vtables.
  • Tools: zig build, -target for cross-compiling, std.Io everywhere since 0.16, and an honest answer about pre-1.0 changes.

Next up: Zig Style Guide, how to name things and lay out code the way the Zig community does.

Quick check

1. An interviewer asks what try foo() is short for. What do you say?
2. Why does a Zig function that allocates usually take a std.mem.Allocator parameter?
3. What happens on u8 overflow in a fast build?