📐 Low-Level Design — interview questions
11 questions. Filter by what the role asks for; open one for the six layers of the answer. The course is where the answers come from.
11 of 11
01A teammate writes `@Service class RequestCounter { private int count; void increment() { count++; } }` and calls it "just a singleton, Spring makes that safe." Does it?Creation patternseasy0-2y02You're handed `new GzipOutputStream(new BufferedOutputStream(new FileOutputStream(path)))` in a code review with no comments. What pattern is this, how do you recognise it on sight, and what would adding "encrypted" as a fourth option cost under this design versus under a subclassing design?Patterns you already useeasy0-2y03A `Rectangle` builder validates each dimension in its own setter — `width(int w) { if (w <= 0) throw ...; this.w = w; return this; }` — and the object still ends up with an invalid combination in production: width and height set fine individually, but the resulting rectangle violates a rule that only makes sense once both are known ("an expedited shipment needs an address", in the lesson's own example). What's the actual rule for where builder validation belongs, and why does per-setter validation miss this?Creation patternsmedium0-2y04A teammate writes `class ReadOnlyOrderRepository extends JpaOrderRepository { public Order save(Order o) { throw new UnsupportedOperationException(); } }` to enforce that a reporting service can't accidentally write. It compiles cleanly and passes code review. What's actually wrong with it, and what should the fix look like?SOLIDmedium3-5y05A `discount(Order o)` method has grown to five `if`/`else if` branches over three sprints, and the newest branch broke a case that was working last week. The team's instinct is "add a comment warning about branch order." What does open/closed say the actual fix is, and when would adding a sixth `else if` still be the right call instead?SOLIDmedium3-5y06A service does `if (order.getStatus() == OPEN && !order.getLines().isEmpty()) { order.setStatus(PAID); order.setPaymentRef(ref); }` — and the exact same check, with a subtly different condition, lives in three other services. A batch job sets an order's status to `PAID` directly, with no lines, and nobody notices for a week. What's the underlying design problem, and what does the fix actually look like in code?Domain modellingmedium3-5y07`@Transactional void placeOrder(Order o) { save(o); notify(o); }` calls `private void notify(Order o) { ... }` on the same class, and `notify` is also annotated `@Transactional(propagation = REQUIRES_NEW)` — the intent being "notify commits in its own transaction, independent of placeOrder's." It doesn't. Why not, precisely, and would making `notify` public fix it?Structural patternshard5-8y08Precisely why can `javac` catch a stronger precondition on an overloaded method's parameter types but not a stronger precondition on an override's *behavior* — and what, exactly, is `invokevirtual` doing at the bytecode level that makes an LSP violation a purely run-time phenomenon?SOLIDhard5-8y09A cache is built as `Map<Order, InvoiceDraft> pendingInvoices = new HashMap<>();`, keyed by the `Order` aggregate itself, with `equals`/`hashCode` overridden to include `status` because "two orders are only really the same if they're in the same state." Orders go into the cache while `OPEN`, and `pendingInvoices.get(order)` starts returning `null` for orders that are definitely still in the map. Walk through exactly what happens, bucket by bucket.Domain modellinghard5-8y10Given `Proxy.newProxyInstance(loader, new Class<?>[]{ PaymentGateway.class, AutoCloseable.class }, handler)`, describe precisely what gets built, when, and why the exact same mechanism means this call would fail outright if `PaymentGateway` were a concrete class instead of an interface — versus why a `final` class specifically breaks Spring's *other* proxying strategy in a completely different way.Structural patternssenior8+y11A codebase has two call sites: `PricingRule.apply(order)` where the concrete implementation is always `StandardPricingRule` in production (a second implementation exists only in tests), and `NotificationChannel.send(msg)` where twelve concrete channel classes are registered and dispatched from one hot loop. A reviewer objects to both interfaces on performance grounds. Which objection, if either, survives contact with how HotSpot actually compiles these call sites?Performance and dispatchsenior8+y