Explaining Your Design and Tech Choices
50 min · Intermediate
Need help? Ask a tutor on WhatsApp (opens in a new tab)After your pitch comes "why did you build it this way?". The interviewer is checking that you chose each tool for a reason, and that you can say what each choice costs.
Architecture walk-through: one request through the layers
Layered architecture splits a backend into controller, service and repository, and each layer talks only to the layer below it.
This plain-Java program sends one request through the three layers and prints what each layer does, including the case where the product does not exist.
Example 1 · One request through controller, service and repository
import java.util.HashMap;import java.util.Map;import java.util.Optional;
record Product(int id, String name, int stock) {}
class ProductNotFoundException extends RuntimeException { ProductNotFoundException(int id) { super("Product " + id + " not found"); }}
class ProductRepository { private final Map<Integer, Product> table = new HashMap<>();
ProductRepository() { table.put(1, new Product(1, "Notebook", 40)); }
Optional<Product> findById(int id) { System.out.println(" repository: SELECT ... FROM products WHERE id = " + id); return Optional.ofNullable(table.get(id)); }}
class ProductService { private final ProductRepository repository;
ProductService(ProductRepository repository) { this.repository = repository; }
Product getProduct(int id) { System.out.println(" service: apply rules, ask the repository"); return repository.findById(id).orElseThrow(() -> new ProductNotFoundException(id)); }}
class ProductController { private final ProductService service;
ProductController(ProductService service) { this.service = service; }
String get(int id) { System.out.println("controller: GET /api/v1/products/" + id); try { return "200 " + service.getProduct(id); } catch (ProductNotFoundException e) { return "404 " + e.getMessage(); } }}
public class LayersDemo { public static void main(String[] args) { ProductController controller = new ProductController(new ProductService(new ProductRepository())); System.out.println(controller.get(1)); System.out.println(controller.get(99)); }}controller: GET /api/v1/products/1
service: apply rules, ask the repository
repository: SELECT ... FROM products WHERE id = 1
200 Product[id=1, name=Notebook, stock=40]
controller: GET /api/v1/products/99
service: apply rules, ask the repository
repository: SELECT ... FROM products WHERE id = 99
404 Product 99 not foundWhat this code does
- Each layer gets the layer below through its constructor, which is exactly what Spring's constructor injection does for you.
- The service throws a domain exception and the controller turns it into a status code. In ShopLite a
@RestControllerAdvicedoes that job and returns a ProblemDetail body. - Saying the trace out loud (controller, service, repository, back up with 200 or 404) is a complete architecture answer in 20 seconds.
Start with the flow, not the folder names: "I have a controller package" says little, while "the service checks stock before the repository saves" shows understanding.
- Controller: maps HTTP to Java, validates input, returns status codes and DTOs.
- Service: business rules and
@Transactionalboundaries. - Repository: database access only (Spring Data JPA or JDBC).
- Around them: a security filter before the controller and
@RestControllerAdvicefor errors.
Order of execution
Order one request
A GET for product 99 finds nothing. Put the steps in the order they happen.
Answering "why this tool?" with need, choice and trade-off
A trade-off is what you give up to get a benefit. A strong "why this tool?" answer names the need, the choice, the trade-off and one alternative.
Example 2 gives the need, choice, trade-off and alternative pattern for three common questions. Example 3 answers a frequent small follow-up: why a price is a BigDecimal.
Example 2 · Need, choice, trade-off, alternative
WHY SPRING BOOT? Need A REST API with database access, validation and security. Choice Spring Boot: starters, auto-configuration, embedded Tomcat. Trade-off Slower start-up and more hidden "magic" than a tiny framework. Instead? Plain servlets or a small framework for a tiny service.WHY MYSQL? Need Products, customers and orders with clear relations, and transactions so stock and order are saved together or not at all. Choice MySQL: relational, ACID transactions, free, widely used. Trade-off Every schema change needs a migration (Flyway in ShopLite). Instead? PostgreSQL would also fit; MongoDB suits data without a fixed shape.WHY JWT? Need Log in once, then call the API from a browser or a mobile app. Choice JWT: the server checks a signed token, no session store needed. Trade-off A token stays valid until it expires, so logout is harder; keep the expiry short. Instead? Server sessions with a cookie, simpler for a single web app.need → choice → trade-off → alternative, about 20 seconds each
What this code does
- Each answer starts from what the project needed, so the tool sounds chosen, not copied.
- Naming the cost (start-up time, migrations, logout) shows you understand the tool, not just its name.
- The alternative proves you compared options; one alternative is enough.
Example 3 · Why price is a BigDecimal, not a double
import java.math.BigDecimal;import java.math.RoundingMode;
public class MoneyChoiceDemo { public static void main(String[] args) { double d = 0.1 + 0.2; System.out.println("double: 0.1 + 0.2 = " + d);
BigDecimal b = new BigDecimal("0.10").add(new BigDecimal("0.20")); System.out.println("BigDecimal: 0.10 + 0.20 = " + b);
BigDecimal price = new BigDecimal("499.00"); BigDecimal gst = price.multiply(new BigDecimal("0.18")).setScale(2, RoundingMode.HALF_UP); System.out.println("GST on 499.00 = " + gst); System.out.println("Total = " + price.add(gst)); }}double: 0.1 + 0.2 = 0.30000000000000004 BigDecimal: 0.10 + 0.20 = 0.30 GST on 499.00 = 89.82 Total = 588.82
What this code does
- A
doublestores binary fractions, so 0.1 and 0.2 are not exact and the sum shows the error. BigDecimalbuilt from aStringkeeps exact decimal digits;setScale(2, RoundingMode.HALF_UP)rounds to paise the way a bill does.- Interview answer: "Money must be exact, so the column is
DECIMAL(10,2)and the Java type isBigDecimal."
"Because it is popular" or "because the course used it" is weak on its own: start from what the project needed, then show you know the cost.
- Spring Boot: starters and auto-configuration; cost: start-up time and hidden configuration.
- MySQL: relations and ACID transactions; cost: a migration for every schema change.
- JWT: stateless login for any client; cost: hard to revoke before it expires.
- JPA: less repeated code; cost: hidden SQL such as N+1 queries.
Revise: JWT structure (opens in a new tab) and Decimal numbers (opens in a new tab).
Fix the error
Fix the money code
Pick the line that compiles and keeps money exact.
Round 1 of 2
Create the price as a BigDecimal. Pick the replacement for line 5.
Error
Price.java:5: error: incompatible types: double cannot be converted to BigDecimal
BigDecimal price = 499.00;
^"What would you change?" and "how would it scale?"
Scaling means handling more users or more data, either by making each request do less work or by running more copies of the app.
This cache answers repeat reads from memory: eight requests for three products should reach the database only three times.
Example 4 · An improvement with a cost: caching product reads
import java.util.HashMap;import java.util.List;import java.util.Map;
class SlowProductRepository { int calls = 0;
String findName(int id) { calls++; return "Product " + id; }}
class CachedProductService { private final SlowProductRepository repository; private final Map<Integer, String> cache = new HashMap<>();
CachedProductService(SlowProductRepository repository) { this.repository = repository; }
String name(int id) { return cache.computeIfAbsent(id, repository::findName); }}
public class CacheChoiceDemo { public static void main(String[] args) { List<Integer> requests = List.of(1, 2, 1, 1, 3, 2, 1, 1);
SlowProductRepository direct = new SlowProductRepository(); for (int id : requests) { direct.findName(id); } System.out.println("Requests: " + requests.size()); System.out.println("Database calls without cache: " + direct.calls);
SlowProductRepository behindCache = new SlowProductRepository(); CachedProductService service = new CachedProductService(behindCache); for (int id : requests) { service.name(id); } System.out.println("Database calls with cache: " + behindCache.calls); }}Requests: 8 Database calls without cache: 8 Database calls with cache: 3
What this code does
computeIfAbsentcalls the repository only for an id that is not in the map yet, so 8 requests for 3 products make 3 database calls.- The cost: if a price changes in the database, the cache still has the old value. A real answer adds "I would clear the entry on update or let it expire".
- A plain
HashMapgrows forever and is not thread-safe; real projects use a cache library such as Caffeine (Section 21, Caching).
"What would you change?" checks that you can judge your own work, and "how would it scale?" asks what you would do with 10 or 100 times more users.
- Improvement answer: weakness, why it matters, the fix, the cost of the fix.
- Scaling order: measure, index, page, cache, more app copies, read replicas.
- Keep the app stateless, so any copy can serve any request.
- Name what you have not tested: "I have not load-tested beyond 100 parallel requests."
"I would rewrite it in microservices" is rarely a good answer for a project this size: small, measured steps sound like real experience. Revise: Cache invalidation and stale data (opens in a new tab) and Vertical vs horizontal scaling (opens in a new tab).
Predict the output
Predict the cache and the pages
Work out what each snippet prints.
Round 1 of 2
A price changes in the database after it was cached. What does the next read show?
Drawing your design on paper in two minutes
A design sketch is a quick box-and-arrow drawing of your system: the client, the app with its layers, and the database.
This is the two-minute sketch of ShopLite: the client at the top, the app with its layers in the middle and the database at the bottom.
Example 5 · The two-minute design sketch
Browser / Postman / mobile app | HTTPS + JSON, JWT in the Authorization header v +---------------------------------------------------+ | ShopLite (Spring Boot) | | security filter -> checks the JWT | | controllers -> HTTP in, DTOs out | | services -> rules, @Transactional | | repositories -> Spring Data JPA | +---------------------------------------------------+ | JDBC through a HikariCP pool v MySQL: categories, products, customers, addresses, orders, order_items, app_user, user_role, refresh_tokenRequest → security filter → controller → service → repository → MySQL, and back as JSON.
What this code does
- The drawing follows the request from top to bottom, so you can talk while you draw.
- Each arrow is labelled with what crosses it; interviewers often point at an arrow and ask "what happens here?"
- Listing the nine tables in the database box answers the usual next question before it is asked.
Draw in the order a request travels, label every arrow with what crosses it (HTTPS and JSON, a JWT header, a JDBC pool) and list the tables inside the database box. Practise until you can draw it in under two minutes while talking.
Interviewers often point at an arrow and ask "what happens here?", so know one sentence for every arrow.
Next step
Check your understanding
11 quick questions