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.
# 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]))None -1Handling empty input and all-negative input shows care.
# State complexity explicitly
# time: O(n), space: O(1)
print('time O(n), space O(1)')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.
