Scrivio Interview library

Behavioral patterns

29 questions in 10 subtopics.

Strategy

CodingFoundations7 min read
This method grows every time marketing adds a customer type:
BigDecimal discount(Customer c, BigDecimal total) {
if (c.type() == GOLD) return total.multiply(new BigDecimal("0.10"));
else if (c.type() == SILVER) ...
}
Refactor it with the strategy pattern using modern Java.
DesignIntermediate7 min read
A multi-tenant Spring service must choose a tax calculation strategy per tenant from configuration, with several implementations as beans. Design the selection mechanism, including startup validation and defaults.
LeadershipAdvanced7 min read
In code reviews across many teams you see strategy interfaces with a single implementation, visitors for three classes, and factories that create one type. How do you set guidance on design pattern use?
LeadershipPrincipal7 min read
Pricing rules are implemented as strategy classes in code. The business wants to change rules without deployments and asks for a rules engine. How do you make this long-term decision?

Observer

ConceptFoundations7 min read
Explain the observer pattern, give Java and Spring examples, and say why java.util.Observable was deprecated.
DebuggingIntermediate7 min read
An @EventListener for OrderPlacedEvent sends a confirmation email. Sometimes customers receive the email although the order transaction rolled back. Explain and fix.
ScenarioAdvanced7 min read
Many modules in your services react to in-process Spring application events. A team proposes replacing all of them with Kafka topics for consistency. How do you decide?

Template method

ConceptFoundations7 min read
Explain the template method pattern and give examples from the JDK and the Servlet API. How does Spring's JdbcTemplate relate to it?
DesignIntermediate8 min read
An AbstractImporter base class has 12 subclasses, each overriding between two and five hook methods. Changes to the base class often break subclasses. How would you refactor it?

Command

ConceptFoundations7 min read
Explain the command pattern with Java examples, and describe how it supports undo.
DesignIntermediate7 min read
Design a background job system where commands are stored in a database and executed by workers, surviving restarts and retried on failure.
LeadershipPrincipal7 min read
Your company implements long-running business processes with home-grown command queues and state machines. Should it adopt a durable workflow engine across teams? Frame the multi-year decision.

Chain of responsibility

ConceptFoundations7 min read
Servlet filters and the Spring Security filter chain follow the chain of responsibility pattern. How does each filter decide to continue or stop, and why does order matter?
DebuggingIntermediate7 min read
A custom authentication filter declared as a Spring @Component and also added to the SecurityFilterChain with addFilterBefore runs twice per request. Why, and how do you fix it?
DesignAdvanced7 min read
Design a shared Spring Boot starter that gives 200 services a standard request filter chain: authentication, tenant resolution, rate limiting and request logging. How do you handle order, extension and rollout?

State

CodingFoundations7 min read
Implement an order lifecycle with states NEW, PAID, SHIPPED and CANCELLED, where only valid transitions are allowed (for example SHIPPED cannot become CANCELLED). Show a Java approach.
DesignIntermediate8 min read
A loan application workflow has 15 states, several approval steps, and regulators require an audit of every transition. Compare the state pattern, an explicit transition table, a state machine library, and a workflow engine.
LeadershipAdvanced7 min read
Six services each implement their own version of the order lifecycle, and orders end up in inconsistent states across systems. Lead the alignment.

Visitor

ConceptFoundations7 min read
Where does the JDK use the visitor pattern? Explain how Files.walkFileTree with a FileVisitor works.
DesignIntermediate8 min read
Compare the visitor pattern with sealed interfaces and switch pattern matching in Java 21 for adding operations to an AST of expression nodes. When is visitor still the better choice?
LeadershipPrincipal8 min read
Lambdas, records, sealed types and pattern matching have absorbed several GoF behavioral patterns: strategy into lambdas, visitor into exhaustive switches, command into records and Runnable. How should a long-lived codebase with years of pattern-heavy code evolve, and what is the risk of mass rewrites?

Iterator

DebuggingFoundations7 min read
This code throws ConcurrentModificationException:
for (Order o : orders) {
if (o.isCancelled()) orders.remove(o);
}
Explain why and give correct alternatives.
CodingIntermediate8 min read
Write an Iterator<Customer> over a REST API that returns customers in pages of 100 with a next-page token. Pages must be fetched lazily. How would you also expose it as a Stream?

Mediator

ConceptFoundations7 min read
Explain the mediator pattern, and describe how DispatcherServlet in Spring MVC acts as a mediator.
ScenarioIntermediate7 min read
A team introduces an in-process command bus: controllers send command objects, and the bus finds a handler by type. After a year, engineers say they cannot follow the code. Evaluate the mediator approach and suggest improvements.
LeadershipPrincipal7 min read
Enterprise service buses acted as company-wide mediators that routed, transformed and orchestrated messages, and many organisations later moved away from them. What lessons apply to central orchestration components such as API gateways and workflow engines today?

Memento

CodingFoundations7 min read
Implement undo for a text editor with the memento pattern in Java, and explain why the memento should not expose the editor's internals.
DesignIntermediate8 min read
A form-heavy application must support undo across many edits on large objects. Compare full-state mementos with storing commands or diffs.
ScenarioAdvanced7 min read
A workflow service saves snapshots of process state so it can roll back after failures, and the snapshot table has grown to billions of rows. How do you rethink this?