Skip to content
Sharp

Design patterns interview questions

Creational, structural and behavioral patterns in Java.

98 questions with answers and explanations · 96 flashcards

Key ideas

  • Creational: Factory Method, Builder, Singleton. Structural: Adapter, Decorator, Proxy. Behavioral: Strategy, Observer, Template Method.
  • Java I/O is the textbook Decorator: new BufferedInputStream(new FileInputStream(f)).
  • Strategy swaps an algorithm behind an interface; with lambdas it's often a functional interface.
  • Singletons need care with threads and serialization. An enum singleton handles both.

Questions (page 2 of 2)

  1. In a system where multiple decorators are applied, how can you ensure that decorating an object does not introduce unexpected side effects?Multiple choiceHard
  2. Why does the Adapter pattern matter in practice?Multiple choiceEasy
  3. A developer argues that a new discount system for an e-commerce site should use static factory methods. What is a potential downside of this approach?Multiple choiceHard
  4. In which scenario would the Factory Pattern be inappropriate?Multiple choiceMedium
  5. How would you explain a key benefit of using the Decorator pattern over subclassing in an interview?Multiple choiceHard
  6. You are implementing a new feature to add premium packaging to orders. How can the Decorator pattern help in ensuring the new feature integrates without…Multiple choiceMedium
  7. Which of the following is an example of using the Decorator pattern in a checkout system?Multiple choiceMedium
  8. In an e-commerce system, you need to apply varying levels of discount to items based on membership status, seasonal sales, and coupon codes. What is a…Multiple choiceHard
  9. You are using the Factory Pattern to create payment processors. However, you notice increased complexity and reduced performance. What might be a reason?Multiple choiceHard
  10. When is the Observer Pattern a better fit than adding more code directly inside CheckoutService?Multiple choiceHard
  11. Which design pattern would you use to delay the loading of a product's detailed description until the user requests it?Multiple choiceEasy
  12. Why does the Proxy pattern in Java applications matter in this design?Multiple choiceEasy
  13. Which purpose does the Factory Pattern primarily serve in design?Multiple choiceEasy
  14. In what way does the Decorator pattern differ from the Adapter pattern?Multiple choiceMedium
  15. Why is it important for a context in the Strategy Pattern to use an interface for strategies?Multiple choiceEasy
  16. How can the Strategy Pattern help in reducing the complexity of a checkout system?Multiple choiceMedium
  17. If an e-commerce platform needs to switch between different inventory management algorithms depending on the region, which pattern is most suitable?Multiple choiceHard
  18. How would you implement a Proxy pattern to control access to a remote service that calculates tax rates in an e-commerce application?Multiple choiceHard
  19. Which pattern should you use to provide a consistent API for multiple e-commerce payment providers?Multiple choiceMedium
  20. Which of the following is a key characteristic of the Proxy pattern in Java?Multiple choiceMedium
  21. What should a payment adapter own when integrating a vendor SDK?Multiple choiceMedium
  22. How would you explain a key difference between the Proxy and Adapter patterns in an interview?Multiple choiceMedium
  23. In a checkout system, shipping cost calculations require data from a new web service, but the current system uses an older API. Which pattern helps integrate…Multiple choiceMedium
  24. How would you explain a sign that the Strategy Pattern is being misused in an application in an interview?Multiple choiceMedium
  25. In terms of software design, what key principle does the Strategy Pattern support?Multiple choiceEasy
  26. How does the Proxy pattern improve performance in a system dealing with large datasets?Multiple choiceMedium
  27. You need to integrate a third-party JSON-based inventory API with an existing XML-based internal system. Which pattern should you employ and why?Multiple choiceHard
  28. Which of the following best describes the Strategy Pattern?Multiple choiceEasy
  29. Why can using the Strategy Pattern in a simple system become a problem in production code?Multiple choiceMedium
  30. Which pattern would you use to expose only certain functionalities of a complex subsystem to a client?Multiple choiceMedium
  31. Before choosing the Strategy Pattern, what question should you ask first?Multiple choiceEasy
  32. How does the Adapter pattern help in integrating third-party libraries?Multiple choiceMedium
  33. In an e-commerce application, you must integrate a legacy payment gateway using an outdated API with your modern payment processing system. Which pattern is…Multiple choiceMedium
  34. How can the Strategy Pattern help in adhering to the Open/Closed Principle?Multiple choiceMedium
  35. What should the Strategy Pattern rely on to allow dynamic strategy selection at runtime?Multiple choiceEasy
  36. How would you explain an anti-pattern related to using design patterns excessively without justification in an interview?Multiple choiceMedium
  37. When refactoring a large switch statement in a checkout system, which pattern provides a cleaner solution?Multiple choiceEasy
  38. Why might using the Proxy pattern be preferred over directly accessing a database in a Java application?Multiple choiceHard
  39. Why might using the Strategy Pattern in a highly dynamic system still lead to complex code management?Multiple choiceHard
  40. Which of the following statements is true regarding the implementation of the Strategy Pattern?Multiple choiceHard
  41. How would you explain a common use case for the Proxy pattern in a database application in an interview?Multiple choiceMedium
  42. In an e-commerce application, how can the Strategy Pattern be over-engineered?Multiple choiceHard
  43. In a checkout system, which scenario best exemplifies the use of the Strategy Pattern?Multiple choiceMedium
  44. How would you explain a typical sign that the Strategy Pattern is being over-engineered in a simple application in an interview?Multiple choiceMedium
  45. Stripe changes its request schema, but checkout already depends only on PaymentGateway. Where should the primary code change occur?Multiple choiceMedium
  46. Which design pattern would you use to allow a payment processor to select between different validation checks dynamically?Multiple choiceMedium
  47. In a system that frequently changes APIs, how can the Adapter pattern help maintain compatibility?Multiple choiceMedium
  48. In an e-commerce application, if you need to track changes to a product's stock level and notify other systems, which design pattern would you apply?Multiple choiceHard