Stage 1 · Code
Programming Foundations
Problem Solving Mindset
Approaching problems systematically: understand, plan, code, test.
Understand the Problem
Before writing code, clarify: What are the inputs? What are the outputs? What are the constraints (time, space, input size)? Are there edge cases? Can you restate the problem in your own words?
- Identify input types, sizes, and constraints.
- Identify expected output format.
- Ask about edge cases: empty input, single element, duplicates, large values.
- Work through 1-2 examples manually.
Plan the Approach
Don't jump to code. Think about: Which data structure fits? What algorithm pattern applies? What's the time/space complexity? Can you do better?
| Pattern | When to Use | Example Problems |
|---|---|---|
| Hash map | Fast lookup, counting, grouping | Two sum, anagrams, frequency |
| Two pointers | Sorted arrays, palindromes | Two sum II, container with most water |
| Sliding window | Subarray/substring with constraint | Max sum subarray, longest substring |
| Binary search | Sorted data, answer space | Search in rotated array, sqrt(x) |
| DFS/BFS | Tree/graph traversal | Path finding, connected components |
| Heap | Top K, priority processing | K closest, merge K lists |
| DP | Optimal substructure + overlapping subproblems | Knapsack, LCS, coin change |
| Backtracking | All combinations/permutations | Subsets, N-Queens, Sudoku |
Code the Solution
- Start with a clean, simple implementation.
- Use meaningful variable names.
- Write helper functions for repeated logic.
- Handle edge cases explicitly.
- Comment complex logic, not obvious code.
Test and Optimize
Trace through your code with examples. Check: empty input, single element, duplicates, large inputs, boundary conditions. Then analyze: can you improve time? Space? Is there a simpler approach?
Common Patterns to Recognize
- Prefix sum: Range sum queries, subarray sum equals K.
- Monotonic stack/queue: Next greater element, sliding window max.
- Fast/slow pointers: Cycle detection, middle of list.
- In-place modification: Use array indices as hash, negation trick.
- Bit manipulation: XOR for duplicates, subsets, power of 2.
- Multi-source BFS: Rotting oranges, walls and gates.
- Union Find: Connected components, cycle detection in undirected graph.
- Topological sort: Course scheduling, alien dictionary.
Interview problems are variations of ~20 core patterns. Focus on recognizing which pattern applies rather than memorizing solutions. When you see 'subarray with constraint' → sliding window. 'Shortest path in unweighted graph' → BFS. 'All combinations' → backtracking.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.