Writing Interview-Grade Code
Intermediate · 16 min read · Low-Level Design, Concurrency & Code Craft
Modelling, validation, exceptions, immutability, testability and common anti-patterns.
In a machine-coding round, two candidates can implement the same design and get opposite results. The difference is code quality: structure, naming, validation, error handling, testability — the things a reviewer notices in the first 60 seconds of reading your code.
Structure the project
Even in a single-file IDE session, grouping classes this way signals layered thinking. Wiring happens in one place (the demo/main), where concrete implementations are chosen and injected.
Model the domain precisely
Value objects over primitives ("primitive obsession" is a common smell):
Validate at the boundary, fail fast
Keep classes and methods small and honest
Depend on abstractions; inject dependencies
Immutability and defensive copies
Thread safety where it matters
If the problem has concurrent actors (gates, users, workers), identify shared state and protect it — see Concurrency for LLD. If not, say explicitly: "this is single-threaded for now; here's what I'd change for concurrent gates."
Demonstrate it works
Common anti-patterns reviewers flag
Time management in the round
Commit to working code first. An elegant design that doesn't run scores lower than a clean, working core with clear extension points.
Interview questions
- Why inject a Clock instead of calling Instant.now()?
- Should 'parking lot full' be an exception or a return value?
Further reading
Key takeaways
- Organise code into model, strategy, repository, service and exception layers, wired in one place.
- Use value objects and enums; make illegal states unrepresentable.
- Validate at boundaries, use specific exceptions, and keep methods small and well named.
- Inject dependencies — including Clock — to stay testable.
- Get a clean core running first; extend afterwards.
Previous: Concurrency for LLD · Next: Code Smells & Refactoring