DSA Patterns

Learn common problem patterns, templates and techniques to solve DSA problems efficiently.

Coordinate two indices through a sequence to eliminate redundant candidate combinations in O(1) per step, dropping O(N²) brute force to O(N).

ArraysStrings+4
28 q65%

Maintain a moving contiguous window across arrays or strings to solve subarray/substring range queries in O(N) linear time, replacing redundant O(N²) or O(N·K) nested recalculations.

ArraysStrings+4
26 q55%

Precompute cumulative sums in a single O(N) pass to answer any contiguous range sum query in O(1) time and detect target subarray sums in O(N) using Hash Map frequency tracking.

ArraysHashing+4
22 q45%

Systematically halve monotonic search spaces at every step to locate target values or determine optimal feasibility boundaries in logarithmic O(log N) time instead of O(N) linear scan.

ArraysDivide and Conquer+4
26 q60%

Move two pointers at different speeds (1x, 2x) to detect cycles and locate middle elements in linked structures.

Linked ListFloyds Cycle+1
14 q70%

Explore solution decision trees, prune invalid paths, and generate permutations, subsets, and combinations.

RecursionBacktracking+2
32 q30%

Solve complex recursive problems by breaking them into overlapping subproblems and memoizing states.

DP on ArraysDP on Strings+2
48 q25%

Construct global optimal solutions through a sequence of locally optimal choices without backtracking.

GreedySorting+2
24 q45%

Maintain strictly increasing or decreasing element order in linear structures to solve next/previous queries in O(N).

StackNext Greater+2
22 q45%

Explore nodes and connected components systematically using queue-based BFS and recursive stack DFS.

BFSDFS+2
36 q55%

Linear ordering of vertices in Directed Acyclic Graphs to resolve prerequisites and schedule task dependencies.

Directed GraphsKahns Algo+1
18 q40%

Tree structure storing character sequences along edges for fast prefix lookups, autocompletion, and word search.

TreesStrings+2
16 q30%

Data structure that tracks partitioned disjoint sets with near-constant time union and find operations.

Connected ComponentsGraphs+1
18 q50%

Use direct bitwise operations (AND, OR, XOR, shifts) for ultra-fast, space-efficient computation.

BitwiseMath+2
20 q20%