Question presented to candidate: "Given a list of daily temperatures, return a list where each entry says how many days you have to wait after that day for a strictly warmer temperature, or 0 if there is none. Please code it in Python and get it under O(n squared)."
What a strong answer should cover:
- The O(nยฒ) brute force first, then the monotonic decreasing stack of indices that answers each day when a warmer day pops it.
- Why it is O(n): each index is pushed once and popped at most once.
- Strict
<for strictly warmer, and the default 0 for days never popped. - The variants: circular (loop
2nwithi % n) and the subset version with a hash map. - Python specifics:
enumerate,[0] * n, a list as the stack withstack[-1],while stack and ..., anddict.get(x, -1)for the subset variant.
Clarifying questions expected:
- "Is warmer strictly greater, or does an equal temperature count?"
- "What should the answer be when no warmer day exists?"
Code / implementation expected: Yes โ a Python function using a list of indices as a monotonic stack.