DSA Study Plan & Complexity

What an SDE2 DSA round actually tests, the Big-O cheat sheet, the 'input size → expected complexity' trick, and a pattern-recognition table to map a problem to a technique fast.

complexitybig-opatternsinterview

Your GPS before writing a single line of code

Imagine you're lost in a city and need to get somewhere fast. You wouldn't just start running in a random direction — you'd look at a map first, find your location, figure out the fastest route, and then move. DSA interviews are the same. Big-O complexity is your map. The constraint in the problem statement (n ≤ 10⁵) tells you exactly which "road" you're allowed to take before you've solved anything.

🧭 The navigator's reflex: Before writing any code, read n. If n ≤ 10⁵ and time limit is 1 second, an O(n²) solution would need ~10¹⁰ operations — roughly 100 seconds. That's impossible. Your map says: take the O(n log n) road. This single reflex separates strong candidates from everyone else.

What SDE2 DSA actually tests

Not "do you know the trick" — it's: can you recognize the pattern, state the complexity, write clean code, handle edge cases, and communicate while doing it. Talk through the brute force, name the bottleneck, then optimize. A working O(n log n) you can explain beats a buggy O(n) you can't.

Big-O complexity: cheat sheet

ComplexityNameTypical sourceExample
O(1)constanthash lookup, array indexdict[key], arr[i]
O(log n)logarithmicbinary search, balanced tree, heap push/popBinary search on sorted array
O(n)linearsingle pass, two pointersFinding max in array
O(n log n)linearithmicsorting, heap-based, divide & conquerMerge sort, heap sort
O(n²)quadraticnested loops, naive pair comparisonBubble sort, brute-force pairs
O(2ⁿ)exponentialsubsets, naive recursion without memoAll subsets of array
O(n!)factorialpermutationsAll arrangements of n items

The input-size → complexity trick

The constraint tells you the target complexity before you've solved anything:

n up toLikely targetExample algorithm
≤ 10–12O(n!) / O(2ⁿ) — backtracking/brute force is fineAll permutations
≤ 20–25O(2ⁿ) bitmask DPBitmask DP over subsets
≤ 500O(n³)Floyd-Warshall
≤ 5,000O(n²)O(n²) DP
≤ 10⁶O(n) or O(n log n)Sorting, BFS, sliding window
≥ 10⁸O(log n) or O(1)Binary search, hash lookup
Use the constraints out loud

"n is up to 10⁵, so an O(n²) ~10¹⁰ won't pass — I need O(n log n) or better." Saying this signals maturity and narrows your options immediately.

Pattern recognition — signal → technique

If you see…Reach forWhy
contiguous subarray/substring, "longest/at most K"Sliding windowGrow/shrink a range without re-scanning
sorted array, pair/triplet summing to targetTwo pointersMove inward from both ends
"search space", "minimum X that works", monotonicBinary search (on answer)Feasibility flips once → binary search the threshold
grid/graph, shortest path (unweighted), levelsBFSLevel-by-level = shortest unweighted path
connectivity, all paths, cycles, componentsDFS / Union-FindExplore all neighbors recursively or track groups
"number of ways", "min/max cost", overlapping subproblemsDPSub-questions repeat → answer each once
top/smallest K, streaming median, merge K listsHeapAlways O(log k) access to current best
all combinations/permutations/subsetsBacktrackingChoose → explore → un-choose
next greater/smaller, matching bracketsMonotonic stackLIFO matches nesting and recency
prefix aggregates, range sumPrefix sumsPrecompute sums so any range is O(1)

How to communicate while coding

  1. Restate the problem + ask about edge cases (empty, duplicates, negatives, overflow).
  2. State brute force + its complexity.
  3. Name the bottleneck and the pattern that removes it.
  4. Code, narrating invariants.
  5. Dry-run a small example; then state final time/space.

Complexity examples in code

Python
# O(n) — single pass, two-pointer style
def two_sum_sorted(arr, target):
    left, right = 0, len(arr) - 1
    while left < right:
        s = arr[left] + arr[right]
        if s == target:
            return [left, right]
        elif s < target:
            left += 1
        else:
            right -= 1
    return []

# O(n log n) — sort then linear scan
def contains_duplicate(nums):
    nums.sort()              # O(n log n)
    for i in range(len(nums) - 1):   # O(n)
        if nums[i] == nums[i + 1]:
            return True
    return False
Java
// O(n) — hash map lookup
import java.util.HashMap;

public class TwoSum {
    public int[] twoSum(int[] nums, int target) {
        HashMap<Integer, Integer> seen = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int complement = target - nums[i];
            if (seen.containsKey(complement)) {
                return new int[]{seen.get(complement), i};
            }
            seen.put(nums[i], i);
        }
        return new int[]{};  // guaranteed to have solution
    }
}
C++
// O(n log n) — sort + binary search
#include <vector>
#include <algorithm>
using namespace std;

bool containsDuplicate(vector<int>& nums) {
    sort(nums.begin(), nums.end());  // O(n log n)
    for (int i = 0; i + 1 < (int)nums.size(); i++) {
        if (nums[i] == nums[i + 1]) return true;
    }
    return false;
}

Think it through

Before solving anything, interviewers expect two instant reflexes: read the complexity of code you're looking at, and read the target complexity off the constraints. Practise both before revealing.

Think it through: Read the complexity, then the constraintFoundation — the reflex every round tests0/5 stages

PROBLEMTwo quick judgments. (a) What's the time complexity of a loop where, for each i, you compare a[i] against every LATER element j? (b) If the problem says n ≤ 10⁵ with a 1-second limit, what complexity must your final solution hit?

  1. 1

    Count the work (part a)

    The inner loop shrinks each time — how many comparisons total?

  2. 2

    Read the constraint backwards (part b)

    Rule of thumb: ~10⁸ simple operations per second. Plug in n = 10⁵.

    unlocks after the stage above
  3. 3

    From target to technique

    You must drop from O(n²) to O(n log n) / O(n). What does that point you toward?

    unlocks after the stage above
  4. 4

    Say it out loud

    What's the sentence that earns marks before you write code?

    unlocks after the stage above
  5. 5

    The takeaway

    Why is this a reflex worth drilling?

    unlocks after the stage above

Practice — climb the ladder

Practice ladder: Big-O and Patterns0/6 solved

Climb in order — every rung assumes the one above it. Solve on LeetCode, then tick it here; progress is saved on this device.

Foundation

read complexity from code
  1. Hash set O(n) vs sort O(n log n) — understand the trade-off.

  2. Classic O(n²) → O(n) via hash map upgrade.

Pattern recognition

map clues to techniques
  1. One pass, track running min — O(n) vs O(n²) brute force.

  2. Count arrays or sorting — two O(n log n) vs O(n) approaches.

  3. Prefix + suffix passes — O(n) time O(1) space (beyond output).

Constraint reading

use n to pick your algorithm
  1. O(n) set-based vs O(n log n) sort — constraints demand O(n).

Check yourself — pattern recognition speed test

Check yourself0/4 answered

1. n = 5,000. Which complexity classes are feasible (< 1 second)?

2. A problem says 'return all subsets'. What complexity do you expect?

3. You see: 'Find the longest contiguous subarray with sum ≤ K'. Which pattern?

4. What's the 'talking out loud' sentence to say when you see n ≤ 10⁵?

Visualize complexity growth

Unknown visualizer: complexity-growth