DSA ยท Chapter 40 of 40

DSA Interview Strategy

Solving the problem is only part of the interview; how you communicate matters just as much. A reliable structure is: clarify, give examples, state a brute force, improve it, code it, test it, then discuss complexity.

Never start typing immediately. Two minutes of clarification often prevents solving the wrong problem.

The 7-step flow

1) Restate the problem. 2) Ask about constraints, duplicates, empty input. 3) Walk one example. 4) Brute force + complexity. 5) Optimise and explain the idea. 6) Write clean code. 7) Dry-run on an example and edge cases.

Pattern cheat sheet

Sorted array โ†’ two pointers/binary search. Subarray โ†’ sliding window or prefix sum. Seen before โ†’ hash map. Shortest path โ†’ BFS. All combinations โ†’ backtracking. Optimal value โ†’ greedy or DP.

Example 1 (python)
# Always test the edge cases you named
def max_of(nums):
    if not nums:
        return None       # empty input
    best = nums[0]
    for n in nums:
        best = max(best, n)
    return best
print(max_of([]), max_of([-3, -1]))
Output
None -1

Handling empty input and all-negative input shows care.

Example 2 (python)
# State complexity explicitly
# time: O(n), space: O(1)
print('time O(n), space O(1)')
Output
time O(n), space O(1)

Finish every answer with its complexity.

Key points

  • Clarify constraints before coding.
  • Say the brute force, then improve it.
  • Dry-run your code on a real example.
  • Always state time and space complexity.
๐Ÿ’ก Note: A clearly explained O(n log n) beats a silent O(n) you cannot justify.

๐Ÿ“ Quick Quiz

1. What should you do first in a DSA interview?

2. 'Subarray with a condition' usually suggests:

3. How should you finish an answer?