Lesson 6 of 8

Code Comprehension · The method · Lesson 6 of 8

Add a feature, then make it faster

After the fix, the round asks you to change working code: add a small feature, then make something faster. How to do both without breaking what already works.

By Akshay, software engineer, India

On this page

Pin the feature down first

Say the feature back as a contract before you write anything, and ask about its edges. “Filter orders by status: only delivered and cancelled, anything else is a 400, and paging works on the filtered list. Is that right?” The questions that matter are about the edges: an unknown value, a missing value, how it combines with what’s there already.

Interviewers often leave an edge out on purpose, to see if you ask. Asking takes ten seconds; building the wrong thing takes ten minutes.

Put it where it belongs

Follow the structure that’s there. If input is checked in the route, check the new input in the route; if the service builds queries, build the new one there. Use the names and style the code already uses. A feature that looks like the rest of the codebase is easy to review; one bolted on in a new style looks like a patch.

Keep old callers working. A new parameter should be optional, with a default that behaves exactly as before, so nothing that calls this code has to change.

Change it safely

  1. Write a test for the new behaviour first. It fails.
  2. Make the smallest change that makes it pass.
  3. Run the old tests, including the one for the bug you just fixed. They still pass.

Prefer extending a table or a type over a special case. A new kind of coupon is one entry in the coupon table and one rule for its type; if (code === 'FEST20') works today and turns into a pile of special cases by the next sale.

Find what’s slow before you speed it up

“Make it faster” starts with “what is it doing too much of?” You won’t have a profiler in the interview, so count instead: how many queries does one request make? How many network calls? How many items does it look at? “Every request makes two queries: one for the page and one that counts every order” is a measurement, and it tells you exactly where to work.

The usual suspects

  • A call per item (N+1). A database or network call inside a loop. Batch it: one call for all the items.
  • A lookup that scans. Finding each row by walking the whole list makes the job rows × items. Build a map once and look up in O(1): rows + items.
  • Asking for more than you need. Counting every row just to know if there’s another page. Fetch one row more than the page instead.
  • Memory that only grows. A cache or log that never forgets anyone. Expire or sweep what’s gone quiet.
  • The wrong data structure. A list where you only ever ask “is it in there?” wants a set.

Say what it costs, and prove nothing changed

Every speed-up costs something: memory for a map, staleness for a cache, complexity for batching. Say it: “this builds a dictionary of all products, so memory grows with the catalogue, but the import goes from minutes to a second.” Then prove the behaviour is the same: the old tests still pass, and a new test checks the improvement itself, for example that a request now makes exactly one query.

Remember
  • Say the feature back as a contract and ask about its edges before writing it.
  • Put it where the codebase would; keep old callers working with optional parameters.
  • Test first, smallest change, old tests still green. Extend a table rather than add a special case.
  • Before optimising, count: queries, calls, items looked at. Then fix N+1s, scans, over-fetching and unbounded memory.
  • Name the trade-off and prove the behaviour didn’t change.
Your progress stays on this device.