CS FundamentalsHardProduct-company design interview
A game renders 1,000,000 trees. Each tree object stores mesh and texture data (about 2 MB, the same for every oak) plus its own x, y position. Which pattern cuts memory, and what is shared?
- A. Flyweight: the mesh and texture (intrinsic state) are shared by all oaks, while position (extrinsic state) is kept per tree or passed in
- B. Prototype: each tree is cloned from one oak
- C. Singleton: there is only one tree object
- D. Proxy: trees are loaded lazily
Correct answer: A. Flyweight: the mesh and texture (intrinsic state) are shared by all oaks, while position (extrinsic state) is kept per tree or passed in
Flyweight splits state into intrinsic (shared, immutable) and extrinsic (context-specific) parts. Cloning with Prototype would still copy 2 MB per tree.
Practise more Software Engineering, Git and Cloud Basics questionsSoftware Engineering, Git and Cloud Basics: notes and key points
More Software Engineering, Git and Cloud Basics questions
- An editor must save snapshots of a document's internal state so it can be restored later, without exposing the document's private fields…
- In an air-traffic system, 20 aircraft objects would each need references to all the others to coordinate landings. Routing every message…
- How many of these differences between Proxy and Decorator are correct? 1. Both implement the same interface as the object they wrap. 2. A…
- A class Invoice has methods calculateTotal(), saveToDatabase() and renderPdf(). Which principle does it violate, and what is the usual fix?
- A Worker interface declares work(), eat() and sleep(). RobotWorker implements it and leaves eat() and sleep() throwing 'not supported'…
- How many of these subclass changes violate the Liskov Substitution Principle? 1. The overriding method accepts a wider range of inputs…