- Turn ambiguous requirements into cases
- Write pseudocode
- Compare solutions by cost
Break the problem down
Define normal examples, boundaries, and errors. If you cannot predict the output for a small input, the rule is not clear yet.
- What do I receive?
- Which constraints exist?
- What must I return?
- What happens with zero, empty, or repeated values?
Pseudocode first
Pseudocode keeps syntax out of the conversation while you are still designing the solution.
FOR each order
IF order is paid
add order.total
RETURN sumCost is part of the solution
Traversing a collection once is usually O(n). Two nested traversals often approach O(n²). Big O describes growth, not today's milliseconds.
const paidTotal = orders
.filter((order) => order.paid)
.reduce((total, order) => total + order.total, 0);Think before coding
Given a list of emails, return the duplicates without repeating them in the result.
Show a hint
A Set can remember which values have already appeared.