Lessons · Python · naming the trade-off
Name both sides of the trade-off
Every choice gave something up. Say what you gained, what you paid, and why the gain matters for this problem.
Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.
What it is for
'Why a dict and not a list?' is not a trick; it is the interviewer checking that you chose rather than reached. An answer with two sides sounds like an engineer. An answer with one side sounds like a habit.
How to think about it
Before you say 'I used X', finish the sentence: 'instead of Y, because here we need Z, and I accept W.' If you cannot name W, you have not understood the choice yet.
Worked example
seen = set()Chosen over a list.
# Gained: membership is O(1) instead of a scan.The side that helps.
# Paid: a little memory, and no order by position.The side you gave up.
# Why here: "we check membership n times, so the scan would cost n squared."The reason it matters for this problem.
for x in [3, 1, 3]:
if x in seen:The O(1) check the trade paid for.
print('dup', x)dup 3seen.add(x)
Your turn
Complete the trade-off for a cache.
# Gained: repeated inputs answer instantly. # Paid: . # Why here: the same keys arrive thousands of times.
Solve one with the tests running
The trap
Answering 'because it is better'. Better at what, at the cost of what? Without the second half the interviewer assumes you do not know it.