Skip to content
Ameen Abozaid
BEG-044

Algorithmic techniques

  • 5 Lessons
  • 57 min of reading
  • 4 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Two pointers
  • sliding window
  • recursion
  • basic backtracking

Learn first

57 min of reading · 4 h of practice (estimate)

Learning outcomes

  1. Apply two pointers to sorted-array problems.
  2. Apply a sliding window to subarray/substring problems.
  3. Write recursive solutions and simple backtracking (subsets, permutations).
  4. Recognize which technique a new problem calls for.

Lessons

  1. 01Two pointers12 min read
  2. 02Sliding window12 min read
  3. 03Thinking recursively11 min read
  4. 04Backtracking11 min read
  5. 05Demo: solving a problem with a sliding window11 min read

Lab and exercises

Lab

BEG-044-LAB1 · Technique drills

Outcomes: 1–4 · Time: ~4 h Solve 8 problems, 2 per technique: two pointers (pair sum in a sorted array; reverse in place), sliding window (best sum of k consecutive items; longest substring without repeats), recursion (sum of digits; power), and backtracking (subsets; permutations). State the time complexity of each solution in a comment. Acceptance: 6 of 8 correct within target complexity.

Exercises (assessment: unseen timed problem set; six exercises)

IDOutcomeTask
BEG-044-EX011Given sorted [1, 3, 4, 6, 9, 11] and target 10, trace the two pointers until the pair is found.
BEG-044-EX021Remove duplicates from a sorted array in place with two pointers; return the new length.
BEG-044-EX032For [2, 1, 5, 1, 3, 2] and k = 3, find the maximum sum of 3 consecutive items with a sliding window.
BEG-044-EX043Write sumDigits(int n) recursively; name the base case.
BEG-044-EX053List all subsets of [1, 2, 3] and say how many permutations it has.
BEG-044-EX064Pick a technique for each: (a) longest run of 1s with at most one 0 flipped; (b) find two numbers that sum to a target in a sorted array; (c) all orderings of 4 letters; (d) count the files in nested folders.
Show solutions
  • EX01 l=0, r=5: 1+11=12, too big, r-- ; 1+9=10, found (1 and 9).
  • EX02 int w = 1; for (int r = 1; r < a.length; r++) if (a[r] != a[w-1]) a[w++] = a[r]; return w; (assuming a non-empty array).
  • EX03 Window sums: 8, 7, 9, 6. Maximum 9 (items 5, 1, 3).
  • EX04 if (n < 10) return n; return n % 10 + sumDigits(n / 10); Base case: a single digit (n < 10).
  • EX05 [], [1], [2], [3], [1,2], [1,3], [2,3], [1,2,3] (8 subsets); 3! = 6 permutations.
  • EX06 (a) sliding window; (b) two pointers; (c) backtracking; (d) recursion.

Quiz

Outcomes: O1 two pointers · O2 sliding window · O3 recursion and backtracking · O4 choosing a technique.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1Two pointers from both ends works best on:O1
  2. 2Two pointers on a pair-sum problem takes:O1
  3. 3In a sorted pair-sum search, if the sum is too small you:O1
  4. 4A sliding window is a:O2
  5. 5The length of window [left, right] is:O2
  6. 6Every recursive method needs a:O3
  7. 7Missing base case usually causes:O3
  8. 8The number of subsets of a 4-item set is:O3
  9. 9Backtracking follows the pattern:O3
  10. 10"Longest substring with no repeated characters" calls for:O4

Practical part

An unseen timed problem set (one problem per technique, stating complexity) plus the lab BEG-044-LAB1 and exercises BEG-044-EX01 to EX06. Pass: at least 3 of 4 timed problems correct within target complexity, lab at 6 of 8, and at least 8 of 10 quiz answers correct.

Common mistakes

Applying two pointers to unsorted data; window bounds off by one.

Assessment

Unseen timed problem set.

Career relevance

Junior technical interviews.

Type to search levels, phases, modules and lessons.