Master technical and career interviews with structured answers—short definition, real examples, pitfalls, and how to answer in 60–90 seconds.
Short answer: Use a hash map from value → index while scanning once. For each number x, check if target − x is already seen; if yes, return both indices. This is O(n) time and O(n) space — the expected answer after you b…
Short answer: Track the minimum price so far while scanning left to right. At each day, compute profit = price − minSoFar and keep the maximum profit. One pass, O(1) extra space. Problem statement prices[i] is the stock…
Short answer: Insert elements into a HashSet while scanning. If an insert fails (already present), return true. Sorting and comparing neighbors also works in O(n log n) with O(1) extra space if sorting in place is allowe…
Short answer: Valid Anagram: count character frequencies (or sort both strings). Group Anagrams: use a frequency signature or sorted string as dictionary key and bucket words together. Complexity Valid: O(n). Group: O(n…
Short answer: Push opening brackets. On a closing bracket, pop and check it matches. At the end the stack must be empty. Covers (), {}, [] in one pass. Sample solution C# bool IsValid(string s) { var st = new Stack<ch…
Short answer: Iterative: three pointers prev, curr, next. Flip curr.next to prev and advance. Recursive: reverse the rest, then point head.next.next = head and head.next = null. Iterative is usually preferred in intervie…
Short answer: Use a dummy head and append the smaller of the two current nodes until one list is exhausted, then attach the remainder. Recursive merge is elegant but uses stack space. Complexity Time O(n + m), Space O(1)…
Short answer: Climbing Stairs: ways(n) = ways(n-1) + ways(n-2) — Fibonacci DP. House Robber: dp[i] = max(dp[i-1], dp[i-2] + nums[i]) — cannot rob adjacent houses. Both reduce to O(1) space with two rolling variables. Com…
Short answer: Maintain inclusive or exclusive bounds consistently. Compute mid carefully (lo + (hi − lo) / 2) to avoid overflow. Decide whether you want lower-bound, upper-bound, or exact match before coding. Complexity…
Short answer: Clarify inputs/outputs/constraints first. Propose brute force, then optimize. Speak while coding, test with an example, and state complexity. Silence is riskier than imperfect code with clear thinking. Inte…
Short answer: Floyd’s tortoise and hare: slow moves 1, fast moves 2. If they meet, there is a cycle. To find the cycle entrance, reset one pointer to head and move both one step until they meet. Complexity Time O(n), Spa…
Short answer: BFS uses a queue and finds shortest paths in unweighted graphs / level order. DFS uses a stack/recursion and is natural for path existence, cycle detection, topological sort, and flood fill. Pick based on w…
DSA & Coding Interviews Coding Interview FAQ · Arrays & Hashing
Short answer: Use a hash map from value → index while scanning once. For each number x, check if target − x is already seen; if yes, return both indices. This is O(n) time and O(n) space — the expected answer after you briefly mention the O(n²) nested-loop brute force.
Given an array of integers and a target, return indices of two numbers that add up to the target. Assume exactly one solution and you may not use the same element twice.
Nested loops comparing every pair — correct but too slow for large n.
One pass with Dictionary/HashMap. Lookup is average O(1).
// C#
int[] TwoSum(int[] nums, int target) {
var map = new Dictionary<int, int>();
for (int i = 0; i < nums.Length; i++) {
int need = target - nums[i];
if (map.TryGetValue(need, out int j)) return new[] { j, i };
map[nums[i]] = i;
}
throw new InvalidOperationException("No solution");
}
Time O(n), Space O(n).
Always start with brute force, then optimize — interviewers score communication as much as code.
DSA & Coding Interviews Coding Interview FAQ · Arrays & Greedy
Short answer: Track the minimum price so far while scanning left to right. At each day, compute profit = price − minSoFar and keep the maximum profit. One pass, O(1) extra space.
prices[i] is the stock price on day i. You may buy once and sell once later. Return the maximum profit (0 if no profit).
int MaxProfit(int[] prices) {
int minPrice = int.MaxValue, best = 0;
foreach (int p in prices) {
if (p < minPrice) minPrice = p;
else best = Math.Max(best, p - minPrice);
}
return best;
}
Time O(n), Space O(1).
Say “running minimum” out loud — it shows you understand the greedy invariant.
DSA & Coding Interviews Coding Interview FAQ · Arrays & Hashing
Short answer: Insert elements into a HashSet while scanning. If an insert fails (already present), return true. Sorting and comparing neighbors also works in O(n log n) with O(1) extra space if sorting in place is allowed.
bool ContainsDuplicate(int[] nums) {
var seen = new HashSet<int>();
foreach (int n in nums)
if (!seen.Add(n)) return true;
return false;
}
HashSet: Time O(n), Space O(n). Sort: Time O(n log n), Space O(1)/O(n).
Ask whether O(1) space matters — that steers you to sorting vs hashing.
DSA & Coding Interviews Coding Interview FAQ · Arrays & Hashing
Short answer: Valid Anagram: count character frequencies (or sort both strings). Group Anagrams: use a frequency signature or sorted string as dictionary key and bucket words together.
Valid: O(n). Group: O(n * k log k) with sort key, or O(n * k) with count key (k = word length).
Prefer count[26] for lowercase English — faster and shows constraint awareness.
DSA & Coding Interviews Coding Interview FAQ · Stacks
Short answer: Push opening brackets. On a closing bracket, pop and check it matches. At the end the stack must be empty. Covers (), {}, [] in one pass.
bool IsValid(string s) {
var st = new Stack<char>();
foreach (char c in s) {
if (c is '(' or '[' or '{') st.Push(c);
else {
if (st.Count == 0) return false;
char o = st.Pop();
if ((c == ')' && o != '(') || (c == ']' && o != '[') || (c == '}' && o != '{'))
return false;
}
}
return st.Count == 0;
}
Time O(n), Space O(n).
This is a common phone-screen opener — nail it quickly and cleanly.
DSA & Coding Interviews Coding Interview FAQ · Linked Lists
Short answer: Iterative: three pointers prev, curr, next. Flip curr.next to prev and advance. Recursive: reverse the rest, then point head.next.next = head and head.next = null. Iterative is usually preferred in interviews for O(1) stack space.
Time O(n). Iterative space O(1); recursive space O(n) call stack.
Draw 3 nodes on the whiteboard before coding — it prevents pointer bugs.
DSA & Coding Interviews Coding Interview FAQ · Linked Lists
Short answer: Use a dummy head and append the smaller of the two current nodes until one list is exhausted, then attach the remainder. Recursive merge is elegant but uses stack space.
Time O(n + m), Space O(1) iterative.
Dummy node avoids special-casing the head — a small habit that impresses interviewers.
DSA & Coding Interviews Coding Interview FAQ · Dynamic Programming
Short answer: Climbing Stairs: ways(n) = ways(n-1) + ways(n-2) — Fibonacci DP. House Robber: dp[i] = max(dp[i-1], dp[i-2] + nums[i]) — cannot rob adjacent houses. Both reduce to O(1) space with two rolling variables.
Time O(n), Space O(1) optimized.
Always define the recurrence in words before writing code.
DSA & Coding Interviews Coding Interview FAQ · Binary Search
Short answer: Maintain inclusive or exclusive bounds consistently. Compute mid carefully (lo + (hi − lo) / 2) to avoid overflow. Decide whether you want lower-bound, upper-bound, or exact match before coding.
Time O(log n), Space O(1).
State the loop invariant: “answer is always inside [lo, hi]”.
DSA & Coding Interviews Coding Interview FAQ · Interview Strategy
Short answer: Clarify inputs/outputs/constraints first. Propose brute force, then optimize. Speak while coding, test with an example, and state complexity. Silence is riskier than imperfect code with clear thinking.
Interviewers hire teammates — communication is part of the score.
DSA & Coding Interviews Coding Interview FAQ · Linked Lists
Short answer: Floyd’s tortoise and hare: slow moves 1, fast moves 2. If they meet, there is a cycle. To find the cycle entrance, reset one pointer to head and move both one step until they meet.
Time O(n), Space O(1) — better than HashSet of visited nodes.
Mention both HashSet and Floyd; then choose Floyd for O(1) space.
DSA & Coding Interviews Coding Interview FAQ · Graphs
Short answer: BFS uses a queue and finds shortest paths in unweighted graphs / level order. DFS uses a stack/recursion and is natural for path existence, cycle detection, topological sort, and flood fill. Pick based on what “best” means.
If the question says “shortest” on an unweighted graph, default to BFS.