Lesson 6 of 27Read before you code
Clarifying Questions
Ask the interviewer the few questions that actually change your solution, and know what each answer changes.
On LeetCode the constraints are printed for you. In an interview they often are not. The interviewer says "merge overlapping intervals" and waits. The missing details (Is the input sorted? Do touching intervals count? Can I change the list?) are left out on purpose, to see whether you ask. Asking the right two or three questions is part of the grade, and the answers decide your algorithm.
Good questions change the code
A clarifying question is only worth asking if a different answer would make you write different code. "What language should I use?" is logistics. "Is the array sorted?" is a real question: yes means two pointers or binary search, no means sorting first or a hash map.
Here are the questions that matter most, grouped, with what each answer changes.
Size and speed
- "How large can the input get?" This is the constraint table from earlier in this track. 100 items means a nested loop is fine; 10^6 means you need n log n or better.
- "Will this be called once, or many times on the same data?" Many queries on fixed data means precompute (prefix sums, a sorted copy, an index) so each query is cheap.
- "Does the data arrive all at once, or as a stream?" A stream means you cannot sort the whole thing; think heaps and running counts.
The values
- "Can values be negative? Zero? Are they integers?" Negatives break sliding windows on sums and "the biggest product is the two largest". Floats change equality checks.
- "Can there be duplicates?" Decides whether you need duplicate skipping (3Sum) or whether a set loses information.
- "Is the input sorted?" The single most valuable question for arrays.
- "What characters can the string contain?" Lowercase letters only means a 26-slot array; any Unicode means a hash map.
The output
- "What should I return if there is no answer?"
-1,None, an empty list, or an exception. Pick the one they want. - "If there are several answers, do you want any one, or all of them? In what order?" Any one lets you stop early; all of them may change the complexity entirely.
- "Can I modify the input, or must I leave it unchanged?" In place allows O(1) extra space; read-only may need a copy.
Strings
String problems hide more unstated rules than any other category, because "character" and "word" mean different things to different people.
- "Is it case-sensitive?" Decides whether
"Aa"is a palindrome or an anagram pair, and whether you lowercase first. - "Which characters count?" For Valid Palindrome, do spaces and punctuation count, or do you skip non-alphanumerics? That is one
isalnum()check, but it changes the answer. - "ASCII, or any Unicode?" ASCII lets you use a 128-slot array. Unicode means a hash map, and it means "length" is ambiguous: an accented letter can be one code point or two, and many symbols are two UTF-16 units in JavaScript.
- "How are words separated?" One space, or any run of whitespace? Leading and trailing spaces?
s.split(" ")ands.split()give different answers in Python on the same input.
What does this print?
print(len("e\u0301"), len("\u00e9"))Graphs and intervals
- "Directed or undirected? Weighted? Can there be cycles? Is it connected?" Each answer swaps algorithms: BFS vs Dijkstra, DFS with or without a visited set, one search or a loop over every node.
- "Do intervals that touch, like
[1, 4]and[4, 5], count as overlapping?" Decides<versus<=in one line, and is the most common Merge Intervals bug.
Match the clarifying question to what the answer changes.
Touching intervals: one question, one character
This is why the touching question matters. Here is a merge loop that treats touching intervals as separate.
How many intervals are left after this merge?
intervals = [[1, 4], [4, 5]]
merged = [intervals[0]]
for start, end in intervals[1:]:
if start < merged[-1][1]:
merged[-1][1] = max(merged[-1][1], end)
else:
merged.append([start, end])
print(len(merged))A sample dialogue
Here is how it sounds when done well. Notice the candidate asks a handful of questions, each tied to a decision, then restates and moves on.
No constraints, no edge cases mentioned. That is deliberate.
Questions for design and class problems
"Design an LRU cache" or "implement a rate limiter class" has a different set of hidden decisions. The examples show the API, not the rules around it. Ask about the ones that change your structure:
- Capacity and eviction. "What is the capacity, and what happens when it is full: evict least recently used, least frequently used, or reject the write?" LRU is a map plus a linked list. LFU needs frequency buckets. Rejecting needs neither.
- Missing keys and bad input. "What does
getreturn for a missing key:-1,None, or raise?" "Can capacity be zero?" - Does
getcount as a use? In an LRU cache it usually does, sogetmust move the key to the front. Asking shows you understand the policy, not just the API. - Concurrency. "Will this be called from several threads?" If yes, you need a lock around the compound operations, and you should say where. Most coding rounds answer "no, single-threaded", but asking is a senior signal, and the follow-up often comes anyway.
- Time. For TTLs and rate limiters: "Is time passed in, or do I read the clock?" A
nowparameter makes the class testable and deterministic, so propose it. - Read or write heavy? If reads dominate, you can do more work on write to make reads O(1).
The Object-Oriented (Low-Level) Design Round lesson in the Interview Prep course goes further on entities, interfaces and extension points.
What not to ask
Every question costs time and signals something. Some signal the wrong thing.
- Anything the examples or prompt already answer. If the example shows
[-2, 1, -3], do not ask whether values can be negative. Point at it instead: "I see negatives in the example, so a sum window will not work." That is a stronger signal than the question. - Anything printed in the constraints. On a shared problem statement with
1 <= n <= 10^5, "how big can n be?" shows you did not read it. - The algorithm itself. "Should I use a heap?" hands the interviewer your job. Propose it and ask whether they want you to go ahead: "I am planning a heap of size k. Does that sound reasonable before I code it?"
- Language trivia you can decide. Naming, whether to use
Counter, whether to add type hints. Choose and move on. - A long checklist in one breath. Ten questions in a row reads as stalling, even if each is valid. Ask the two or three that matter, and fold the rest into stated assumptions.
When the answer is "your choice"
Interviewers hand decisions back more often than you might expect, sometimes to see whether you can make a call without a spec. That is the job at work too. A good pattern:
- Pick the simplest assumption that is still realistic. Empty input returns an empty result. Values fit in 64-bit integers. Strings are ASCII. Inputs are valid unless the problem is about validation.
- Say it out loud with its consequence. "I'll assume ASCII, so I can use a fixed array of 128 counts. If we need Unicode I would switch to a dict."
- Write it down. A one-line comment at the top of your code (
# assume: non-empty, ASCII) keeps you and the interviewer aligned, and reminds you to test it. - Keep it cheap to change. Put the assumption in one place, such as the merge comparison or the counts structure, so that if the interviewer later says "now support Unicode", it is a one-line edit. Handling Follow-Up Questions picks up from there.
How many, and when
Ask after you restate the problem and before you design. Two to four questions is normal. More than that starts to look like stalling, so drop any question whose answer would not change your code.
If the interviewer says "you decide", that is fine too. State an assumption out loud and move on: "I'll assume the values fit in a 64-bit integer and the input can be empty." Stated assumptions count as well as answered questions.
Put the opening of an interview answer in order.
- Tap the steps below in the right order.
Practice
Write down the questions you would ask before solving this, and the answer you will assume for each. The prompt answers some of them; check which.
Merge Intervals
Given a list of intervals [start, end], merge every group that overlaps and return the result sorted by start. Intervals that touch (one ends where the next starts) count as overlapping.
[[1, 3], [2, 6], [8, 10], [15, 18]] → [[1, 6], [8, 10], [15, 18]]
[[1, 4], [4, 5]] → [[1, 5]]
[[4, 7], [1, 4]] → [[1, 7]] (input may be unsorted)Constraints
0 <= intervals.length <= 10^40 <= start <= end <= 10^4
- clarifying question
- A question whose answer changes your code: sorted?, negatives?, duplicates?, empty?, in place?
- stated assumption
- When the interviewer leaves a detail to you, say what you assume out loud and make the code match.
- touching intervals
- Intervals like
[1, 4]and[4, 5]. Whether they merge decides<versus<=. - precompute
- Do work once up front (sort, prefix sums, an index) so many later queries are cheap.
- eviction policy
- The rule a bounded cache uses to choose what to drop when full, such as least recently used or least frequently used.
- code point
- One Unicode value. What a reader sees as one character can be one or several code points.
- stream
- Data that arrives over time. You cannot sort it all, so you keep running structures like heaps or counts.