A strong answer
Many problems get faster when you spend memory: a hash set turns an O(n²) duplicate check into O(n), a cache avoids recomputing results, an index avoids scanning a table, and dynamic programming stores subproblem answers. Usually that trade is worth making, because memory is cheaper than time, and quadratic time stops scaling long before memory runs out.
I would not make it when:
- The data is huge and the helper structure is as big as the input: a hash set of a billion keys may not fit, while sorting in place and scanning neighbors will.
- Memory is multiplied by concurrency. O(n) extra per request, times a thousand concurrent requests, can exhaust a server that handles each one comfortably.
- The memory isn't free in time either. Big allocations cost allocation and garbage-collection time, and a structure that doesn't fit in cache can be slower than recomputing.
- It is stack memory. Deep recursion uses O(depth) stack space and can overflow; an explicit stack or an iterative version avoids that.
I also look for memory that is spent by accident: copies from slicing or sorting, strings built by repeated concatenation, and helper collections that live longer than they need to. And I state both costs in my answer, for example "O(n) time and O(n) space, or O(n log n) time and O(1) extra space if memory is tight".
What the interviewer is listening for
- Concrete examples of the trade in both directions.
- Awareness of concurrency, stack depth and allocation as real costs.
- Stating time and space together.
Common mistakes
- Ignoring space entirely.
- Counting the input as extra space, or forgetting the recursion stack.
- Assuming the faster algorithm is always better, even when it can't fit in memory.