# oop & design patterns

// [23] real interview questions. Answers and sources live in the practice app.

practice this topic
  1. What is the Singleton pattern, and when is it actually legitimate?(junior)
  2. Why is a naive lazy-initialized Singleton broken under concurrency? Show the failure.(mid)
  3. Compare the ways to make a Singleton thread-safe: synchronized getInstance, eager static init, double-checked locking, enum/holder idiom.(senior)
  4. Your test suite passes in isolation but fails when run together, and the culprit is a Singleton. Why do singletons poison tests, and what's the structural fix?(senior)
  5. Explain the Observer pattern with a real use case. What's the main pitfall in practice?(mid)
  6. A method has grown a switch statement over 'shipping type' repeated in three places. Which pattern fixes this, and how?(mid)
  7. How does the Decorator pattern add behavior without inheritance, and why is it better than subclassing here?(mid)
  8. 'Prefer composition over inheritance' — explain the principle with a concrete refactor where inheritance went wrong.(mid)
  9. What is dependency injection, and what concretely improves when you use it?(mid)
  10. Why is calling an overridable (or abstract) method from a constructor dangerous?(senior)
  11. Static vs non-static (inner) nested classes: what's the difference, and what's the memory-leak trap?(senior)
  12. When would you choose a TreeSet over a HashSet? What are you trading?(mid)
  13. Is List<String> a subtype of List<Object> in Java? Explain generic variance and the ? extends / ? super wildcards.(senior)
  14. What is reflection, why do frameworks depend on it, and what are its costs?(mid)
  15. High cohesion, loose coupling — define both, and name the smells that tell you a codebase lacks them.(mid)
  16. order.getCustomer().getAddress().getCity() — which principle does this chain violate, and when should you actually care?(mid)
  17. What is Inversion of Control — beyond just being another name for dependency injection?(senior)
  18. Why is global or static mutable state considered harmful? Show the concrete failure modes.(mid)
  19. You inherit a 6000-line class named AppManager that everything depends on. What anti-pattern is this, and how do you dismantle it without a rewrite?(senior)
  20. Active Record vs Data Mapper for persistence — what are the trade-offs, and what is an 'anemic domain model'?(senior)
  21. Tony Hoare called the null reference his 'billion-dollar mistake'. How do you design code so NullPointerExceptions stop happening?(senior)
  22. What does DRY actually mean — and when is duplication the better engineering choice?(mid)
  23. What do access modifiers give you — and why isn't a getter/setter pair for every field 'encapsulation'?(junior)