Notifications

No notifications

/Phase 4

Practice & Projects

Previous

Practice & Projects — Sharpening Your Python Skills šŸ†

Moving from tutorials to real problem-solving requires mastering patterns and building projects. This module covers the most common coding interview patterns in Python and gives you a roadmap for practical projects.

Key Problem-Solving Patterns

PatternWhen to UsePython Tool
Two PointersSorted arrays, palindromesleft, right = 0, len(a)-1
Sliding WindowSubarrays/substrings of size kcollections.deque
Hash MapFrequency, lookup, duplicatesdict, Counter, defaultdict
StackMatching brackets, monotoniclist as stack (append/pop)
BFS / DFSGraphs, trees, connected componentsdeque for BFS, recursion for DFS
Binary SearchSorted data, search space reductionbisect module
GreedyOptimal local choicesSort + iterate
Dynamic ProgrammingOverlapping subproblems@lru_cache or table

Problem-Solving Framework

1. UNDERSTAND — Restate the problem, identify inputs/outputs
2. EXAMPLES  — Work through 2-3 examples by hand
3. APPROACH  — Pick a pattern, analyze time/space complexity
4. CODE      — Write clean, modular Python
5. TEST      — Verify with edge cases (empty input, single element, large N)

Project Ideas for Portfolio

ProjectSkills Used
Web Scraperrequests, BeautifulSoup, file I/O
CLI Task Managerargparse, JSON, file I/O, OOP
Data Analyzerpandas, matplotlib, CSV processing
REST API Clientrequests, error handling, json
Markdown-to-HTML ConverterRegex, file I/O, string manipulation

Competitive Programming Tips

  • Read the constraints first — they hint at the expected time complexity
  • Use sys.stdin for fast input in competitive programming
  • Python's built-in sorted() uses Timsort (O(n log n)) — very efficient
  • @lru_cache turns recursion into memoized DP with one decorator
> Tip: Aim for 2-3 problems per day. Focus on understanding the pattern, not memorizing solutions.

On this page

Detailed Theory

Knowing the syntax doesn't mean you can *solve problems*. The gap between "I learned Python" and "I can build / interview with Python" is closed only by deliberate practice — LeetCode, HackerRank, real projects, code reviews. This page is your roadmap.

What "Practice" Actually Means

Reading code teaches you syntax. Solving problems teaches you judgement: choosing the right data structure, recognising patterns, debugging under pressure. The skill compounds — the 200th problem feels easier than the first 20 combined.

Goal: solve problems *consistently*, not heroically. 1 problem a day for a year beats 20 in a weekend then nothing.

A Reliable Problem-Solving Process

1. Read twice. Identify inputs, outputs, constraints, and at least one example by hand. 2. Restate the problem in your own words. 3. Brute force first. Even an O(n³) solution is something. Don't let perfect block any. 4. Optimise. Where's the wasted work? Repeated computation → cache. Repeated lookups → hash map. Sorted scan possible → sort. 5. Code carefully. One small, named helper at a time. 6. Test on edge cases. Empty input, one element, duplicates, negatives, max size. 7. Reflect. After solving, write 2 lines: pattern, mistake, what to remember.

Constraint-Driven Complexity Targets

Look at the input size to pick a target:

nTargetCommon patterns
≤ 10brute forcerecursion, permutations
≤ 20O(2ⁿ)bitmask DP, backtracking
≤ 1,000O(n²)nested loops, DP grid
≤ 10⁵O(n log n)sorting, binary search, divide & conquer
≤ 10⁶O(n)hash map, two pointers, sliding window, prefix sums
≤ 10⁹O(log n) / O(√n)math, binary search on answer

Beginner Mistakes to Skip

1. Watching solutions before trying. You learn by struggling for 20–30 minutes first; *then* peek. 2. Skipping easy problems. "Easy" tags exist to drill fundamentals. Get fast there before chasing hards. 3. No edge-case checklist. Empty, single, duplicates, sorted reverse, max size — always. 4. Not measuring. Time yourself. Can you re-solve a problem you saw last week in 15 minutes? 5. Cargo-culting cool tricks. A clean O(n²) you understand beats an O(n) you can't explain in an interview. 6. Only LeetCode, no projects. Real projects teach what interviews can't — messy data, vague specs, deployment.

Intermediate: The Patterns That Cover ~80%

  • Hash map: unique counts, two-sum, frequency, group-by.
  • Two pointers: sorted array problems, pair sums, dedupe.
  • Sliding window: longest/shortest substring with constraint.
  • Binary search: sorted arrays, "binary search on the answer".
  • BFS / DFS: trees, graphs, grids, connected components.
  • Recursion + memoization → DP: optimal subproblems.
  • Stack / monotonic stack: parentheses, next-greater-element.
  • Heap / priority queue: top-K, scheduling, k-way merge.
  • Backtracking: permutations, combinations, sudoku, n-queens.
  • Greedy: interval scheduling, change-making (when valid).
Learn one pattern per week. Solve 5–10 problems each before moving on.

Intermediate: Python Cheats for Interview Speed

from collections import Counter, defaultdict, deque
import heapq, bisect, math, itertools, functools

Counter("abracadabra").most_common(2) bisect.insort(sorted_list, x) # binary insert heapq.nsmallest(3, nums) # top-3 smallest functools.lru_cache(maxsize=None) # memoise defaultdict(list) # group-by deque(maxlen=k) # fixed-size sliding window itertools.pairwise([1,2,3,4]) # (1,2),(2,3),(3,4) for i, x in enumerate(arr): ... for a, b in zip(arr, arr[1:]): ... int("abc123".isdigit()), s.count("a"), sorted(s)

These 10 lines cover an embarrassing fraction of LeetCode mediums.

Intermediate: Reading Test Failures

When your code fails:

1. Run the failing example by hand, line-by-line, with a notebook. 2. Print intermediate state, not just the final return. 3. Reduce to the smallest failing input. 4. Fix the root cause, not the symptom.

If you've stared for 15 minutes with no progress, take 5 minutes off the screen. New ideas surface in the silence.

Advanced: Spaced Repetition for Problems

Keep a personal log: problem name, pattern, mistake, link. Re-solve flagged problems after 1 day, 1 week, 1 month. This *compounding review* is what separates people who "have done" 500 problems from people who can solve them under pressure.

Tools: a spreadsheet, an Anki deck, or a simple markdown file.

Advanced: Beyond Algorithms — Real Projects

Interviews aren't the only judge. Build:

  • A CLI (argparse / typer) that does something useful.
  • A web API (FastAPI / Flask) with a small DB.
  • A scraper + dashboard (requests, pandas, streamlit).
  • A data pipeline (read → clean → transform → export).
  • One ML notebook end-to-end on a Kaggle dataset.
Projects teach what LeetCode can't: error handling, deployment, dependency management, working with messy real data.

Advanced: Mock Interviews & Code Reviews

  • Mock with friends or platforms (Pramp, interviewing.io). Talking *while* coding is its own skill.
  • Open-source contributions — even doc fixes — give you real-world code review feedback.
  • Read other people's solutions after you solve. Note one trick per problem.
The loop: solve → explain out loud → read better solutions → simplify yours.

Advanced: Curated Problem Tracks

  • NeetCode 150 / Blind 75 — the canonical interview list.
  • LeetCode "Top Interview 150" — official curated set.
  • Cracking the Coding Interview (CTCI) — textbook patterns.
  • HackerRank "30 Days of Code" — gentle ramp-up.
  • Project Euler — maths-flavoured, sharpens optimisation.
  • Advent of Code — yearly, fun, varied difficulty.
Pick one track. Finish it. Then specialise.

Practice Path

1. Solve Two Sum from scratch in O(n) using a dict; explain to yourself why a brute O(n²) version fails on n = 10⁵. 2. Solve a sliding window problem (e.g., longest substring without repeating chars); practice growing/shrinking the window. 3. Re-solve a problem you struggled with 3 days ago; aim for under 15 minutes. 4. Pick a 1-week mini-project (CLI, scraper, or small API) and ship it to GitHub with a README.