CS FundamentalsVery hardProduct-company design interview
A compiler has a stable set of AST node classes (Number, BinaryOp, Call, Variable) but keeps adding new operations: type-check, pretty-print, optimise, generate code. Which pattern fits, and what is its main drawback?
- A. Strategy; each operation becomes a separate algorithm, but nodes cannot be reused
- B. Template Method; new operations are easy but new node types need a new template
- C. Composite; it makes trees easy, but every new operation still edits every node
- D. Visitor; new operations are added without touching the node classes, but adding a new node type forces a change to every visitor
Correct answer: D. Visitor; new operations are added without touching the node classes, but adding a new node type forces a change to every visitor
Visitor uses double dispatch so each operation lives in one visitor class. That suits a stable element hierarchy with growing operations; the trade-off is that a new element type needs a new visit method in all visitors.
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
- A file-manager must compute the total size of a folder, where a folder may contain files and other folders, and client code should call…
- A text editor needs multi-level undo and redo, and also wants to queue and log user actions such as 'insert text' or 'delete line'. Which…
- A vending machine's behaviour for insertCoin() and selectItem() differs completely in the states Idle, HasCoin, Dispensing and SoldOut…
- A report class defines generate() as: fetchData(); format(); export(). Subclasses PdfReport and CsvReport override only format() and…
- An app must create a consistent family of UI widgets (Button, Checkbox, Menu) for either a Light or a Dark theme, never mixing a Light…
- 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…