tezvyn:

What are Swift generics, why useful, and write a swap function?

AI-drafted, machine-checkedSource: docs.swift.orgintermediate

This tests parametric polymorphism and type-safe reuse. A strong answer defines generics as placeholder types for reusable code, then writes a swap<T> function using inout parameters. Red flag: confusing generics with Any or omitting inout.

WHAT THIS TESTS: This question evaluates whether you understand parametric polymorphism and can articulate the difference between compile-time abstraction and runtime type erasure. The interviewer wants to see that you know generics enable type-safe reuse without sacrificing performance or resorting to casting. They also want to see if you understand value semantics and mutation in Swift, because the swap example requires inout parameters.

A GOOD ANSWER COVERS: First, define generics as placeholder types that let a single function, struct, class, or enum work with any concrete type while preserving type information at compile time. Second, explain the practical benefits: code reuse without duplication, compile-time type checking that prevents invalid operations, and performance because Swift can generate specialized code behind the scenes. Third, write the swap function exactly as func swap<T>(_ a: inout T, _ b: inout T) { let temp = a; a = b; b = temp } and explain that the T placeholder guarantees both parameters are the same type. Fourth, mention that inout is required because Swift uses value semantics by default, so the function must mutate the caller's original bindings.

COMMON WRONG ANSWERS: A major red flag is conflating generics with the Any type or with runtime casting; saying you could use Any and downcast misses the point of compile-time safety. Another red flag is omitting inout and writing a function that returns a tuple instead of swapping in place, which shows confusion about value semantics versus reference semantics. A third warning sign is claiming that generics always incur runtime overhead or boxing; in Swift, generics are often specialized by the compiler.

LIKELY FOLLOW-UPS: The interviewer may ask how generics differ from protocols or protocol-oriented programming, or when you would use type constraints such as T: Equatable. They might also ask about generic specialization, the difference between compile-time and runtime polymorphism, or how generics interact with associated types in protocols. You should be ready to discuss opaque types or primary associated types if the role is staff-level.

ONE CONCRETE EXAMPLE: Imagine a non-generic swap for Int: func swapInts(_ a: inout Int, _ b: inout Int) { let temp = a; a = b; b = temp }. Without generics, you would need an identical function for String, Double, and every custom struct. By writing func swap<T>(_ a: inout T, _ b: inout T) { let temp = a; a = b; b = temp }, the compiler enforces that a and b share the same type, prevents mixing types, and generates optimized machine code for each concrete use at compile time.

Read the original → docs.swift.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.