Skip to content
← KR7Tech Data Structures & Algorithms · Visual Practice

Learn patterns, then solve problems

DSA coding practice

Read the problem. Write your code. Run a test. Compare the solution when you are ready.

Pattern guide & problem-solving method
How to use this page: try each problem before opening the solution. Reference methods may need imports, a main function, or node definitions before they can run.

1. A repeatable problem-solving method

① Understand

Restate the input/output. Ask about constraints, duplicates, empty input, ordering, and whether mutation is allowed.

② Build a tiny example

Trace one normal case, one boundary case, and one case that could break a naive approach.

③ Choose a pattern

Look for a pair, a range, nested structure, repeated subproblem, graph reachability, or a top-K requirement.

④ State an invariant

Explain what remains true as pointers, window bounds, visited sets, or DP states change.

⑤ Prove + analyze

Explain why the algorithm cannot miss a valid answer; state time and extra space honestly.

⑥ Test + refine

Check empty, singleton, duplicates, all-equal, boundaries, overflow, cycles, and repeated calls.

2. Visual pattern map

These visual sketches are mental models. Open a problem below to see its own example trace and implementations.

1Hash map

Remember what has appeared so far.

27→9targetseen?

Store value → index; look up the complement.

2Two pointers

Use order or a partition to eliminate candidates.

L135Rmove inward

Change one pointer based on the current sum/comparison.

3Sliding window

Maintain a valid contiguous range.

abcd→bc

Expand right; shrink left when the invariant breaks.

4Stack / monotonic stack

Keep unresolved items until a match arrives.

7374→76resolves

Pop smaller unresolved values when a warmer day appears.

5Binary search

Discard half of a monotonic search space each step.

12479mid

Maintain a precise inclusive or half-open interval.

6Tree / graph traversal

Visit nodes while tracking what is discovered.

A↙B↘C

DFS for depth/path reasoning; BFS for layers/shortest unweighted paths.

7Dynamic programming

Reuse answers to smaller subproblems.

dp[0]→dp[1]→dp[i]

Define state, recurrence, base case, and evaluation order.

8Backtracking

Explore choices and undo them when returning.

root↙choose↘undo

Choose → recurse → un-choose; prune impossible branches.

9Heap / top-K

Keep only the best K candidates as data streams in.

input→min-heapsize K

A bounded min-heap keeps the K largest values.

10Intervals / greedy

Sort by a boundary, then combine compatible ranges.

[1,3][2,6]→[1,6]

Sort once; make a locally safe choice with a clear invariant.

Problem & code workspace

First 10 DSA problems are free. Pro unlocks the complete set and remote code execution. Your drafts stay in this tab.

Select a problem to start practicing.