# Revision and Interview Questions — Zig

Source: https://www.geekswithgeeks.com/en/zig/p-revision

> Recall Zig concepts quickly for exams and interviews.

## Cheat sheet

**Philosophy**: no hidden control flow, no hidden allocations, explicit errors, comptime instead of macros, C interop, pre-1.0 (std changes). **Toolchain**: `zig run`, `build-exe`, `test`, `fmt`, `init`, `zig build` (`run`, `test`), build modes Debug, ReleaseSafe, ReleaseFast, ReleaseSmall, ZLS. **Basics**: `const`/`var`, arbitrary-width integers, `usize`, comptime_int, explicit casts (`@intCast`, `@floatFromInt`, `@as`), `undefined`, unused-variable errors. **Control**: if/switch/blocks as expressions, captures, `while` with continue expression, multi-object `for (a, 0..)`, labelled blocks and breaks, exhaustive switch. **Data**: arrays `[N]T`, slices `[]T`, strings as `[]const u8`, sentinels `[:0]u8`, `std.mem.eql`, pointers `*T`, `*const T`, `[*]T`, optional pointers `?*T`. **Types**: structs with defaults and methods, files as structs, `@This()`, enums, tagged unions `union(enum)`, tuples. **Errors**: error sets, `!T`, `try`, `catch`, `orelse`, `.?`, `defer`, `errdefer`, error return traces. **Memory**: `std.mem.Allocator`, DebugAllocator, ArenaAllocator, FixedBufferAllocator, page_allocator, smp_allocator, ownership conventions, unmanaged ArrayList, StringHashMap and key lifetimes. **Comptime**: comptime blocks and parameters, types as values, generic `fn T(comptime) type`, `@typeInfo`, `inline for`, `@field`, `@compileError`, `anytype`. **Systems**: overflow panics, `+%`, `+|`, `@addWithOverflow`, safety checks, `@cImport`, `export fn`, `extern struct`, cross-compilation, `zig cc`. **Build**: build.zig modules and steps, build.zig.zon and `zig fetch`, `std.testing.allocator`, `checkAllAllocationFailures`. **Advanced**: threads, Mutex, atomics, packed structs, `@Vector`, buffered `std.Io.Writer` and `flush`.

## Common interview questions

Answer each with a short code example.

```text
1. What does "no hidden control flow" and "no hidden allocations" mean in Zig?
2. How does Zig handle errors without exceptions? Explain try, catch and errdefer.
3. What is the difference between an array, a slice and a many-item pointer?
4. Why do Zig functions take an Allocator parameter? Name three allocators and their uses.
5. What is comptime, and how does Zig implement generics with it?
6. How are optionals represented, and why can't ordinary pointers be null?
7. What do the four build modes change, especially regarding safety checks?
8. How does Zig handle integer overflow, and what are wrapping and saturating operators?
9. How does Zig interoperate with C in both directions?
10. Why is Zig popular as a cross-compilation toolchain (zig cc)?
11. What is a tagged union and why is switch exhaustiveness useful?
12. How do you detect memory leaks in Zig tests?
```

## Compare thoughtfully with C and Rust

Interviewers often ask how Zig compares: more explicit and simpler than C++ or Rust, safer defaults than C, but without Rust's compile-time memory-safety guarantees. A balanced answer shows real understanding.

**Quiz:** Which Zig keyword runs cleanup only when a function returns an error?

- [ ] defer
- [ ] catch
- [ ] unreachable
- [x] errdefer

*Answer:* errdefer. errdefer is the tool for cleaning up partial work on failure.
