Advanced Topics in Computer Science

Unit 1: Recursion

Students learn to solve problems by defining smaller versions of the same problem, then reason about call stacks, base cases, and recursive correctness.

Unit Focus

What this unit is really teaching

The unit turns recursion from a mysterious trick into a reliable design pattern for strings, arrays, linked structures, trees, and backtracking.

Key Topics

  • +Base cases, recursive cases, and progress toward termination
  • +Call stack tracing and return-value reasoning
  • +Recursive string, array, and ArrayList algorithms
  • +Divide-and-conquer thinking and recurrence intuition
  • +Backtracking patterns for search and constraint problems
  • +Comparing recursive and iterative solutions for clarity and efficiency

Practice Work

Implementation and analysis tasks

Student task

Build recursive solutions for palindrome checks, binary search, and nested folder traversal.

Student task

Implement a small backtracking puzzle solver with trace output.

DSA Assignment Bank

30 interview-style algorithm questions for this unit

Code Scholars provides structured assignments, implementation support, edge-case review, and runtime analysis coaching so students learn the concept rather than only memorizing a solution.

1

Recursive branching

Count grid paths with blocked cells

Write a recursive method that counts ways to move right or down through a grid while avoiding blocked cells and stopping cleanly at boundaries.

2

Backtracking

Generate valid symbol strings

Generate all strings with n pairs of matched symbols, using counts of opened and closed symbols to prevent invalid prefixes.

3

Recursive search

Split a string into dictionary words

Given a set of valid words, decide whether a string can be split into valid pieces and explain where repeated subproblems appear.

4

Include or exclude recursion

Find every subset with a target sum

Given an array of positive integers, recursively choose or skip each value and collect subsets whose sum equals a target.

5

Divide and conquer

Trace recursive binary search

For a sorted array, trace the low, high, and mid values for binary search and explain the base case for a missing target.

6

Two-pointer recursion

Recursive palindrome with cleanup

Check whether a phrase is a palindrome after ignoring spaces and case, without creating a full reversed copy.

7

Flood fill

Count islands recursively

Use recursion to count connected land regions in a small grid, marking cells safely so the same cell is not counted twice.

8

Backtracking with visited state

Solve a small maze

Return one path through a maze from start to finish, using a visited grid to prevent cycles.

9

Recursive construction

Generate phone keypad strings

Given digits mapped to letters, recursively generate all possible letter strings and explain the branching factor.

10

Recursive structure

Compute nested list depth

Given a simplified nested-list object, compute its maximum depth and describe the result for an empty nested list.

11

Recursive pointer update

Reverse a linked list recursively

Reverse a singly linked list using recursion and explain which reference becomes the new head.

12

Tree recursion

Flatten a tree into preorder list

Traverse a binary tree recursively and append values to a list in preorder while preserving the original tree.

13

Constrained recursion

Build all increasing sequences

Generate all length-k increasing sequences from 1 to n and prune choices that cannot complete the sequence.

14

Optimal substructure

Find the minimum recursive path sum

Given a grid of nonnegative costs, recursively compute the cheapest path to the bottom-right using only right and down moves.

15

Recurrence design

Count ways to climb with variable steps

Count ways to reach step n using steps of size 1, 2, or 3, and identify why a plain recursive solution repeats work.

16

String recursion

Recursive run-length encoding

Compress consecutive characters into character-count groups using recursion over the current index and run length.

17

Recursive state tracking

Partition an array recursively

Determine whether an array can be split into two groups with equal sum by recursively assigning each value to one side.

18

Recursive comparison

Find first recursive mismatch

Compare two strings recursively and return the first index where they differ, including behavior when one string ends first.

19

Backtracking and swapping

Generate permutations without repeats

Generate all permutations of distinct values using in-place swaps, then restore the array after each recursive call.

20

Backtracking with duplicate control

Generate permutations with duplicates

Modify permutation generation so duplicate input values do not produce duplicate output arrangements.

21

Tree recursion

Evaluate a recursive expression tree

Given a binary expression tree, recursively evaluate operators and operands while rejecting malformed operator nodes.

22

Divide and conquer

Find recursive majority candidate

Split an array into halves, find a candidate in each half, and verify which candidate appears most often.

23

Memoized recursion

Count paths in a directed acyclic graph

Given a DAG, recursively count paths from a start node to an end node and explain why memoization is safe.

24

Recursive parsing

Recursive string expansion

Expand a small pattern such as repeated grouped text by recursively processing bracketed sections.

25

Pruned recursion

Find kth generated string

Generate lexicographic strings over a small alphabet, but skip whole branches when their size is smaller than k.

26

Flood fill boundaries

Recursive matrix region fill

Recolor a connected region in a matrix and explain the four checks needed before each recursive call.

27

Constraint search

Backtrack a course schedule

Assign students to limited project teams recursively while respecting capacity and prerequisite constraints.

28

Call-stack reasoning

Trace recursive returns

Given a recursive method that returns a value after two calls, draw the call tree and compute the final return value.

29

Stack simulation

Convert recursion to iteration

Rewrite a recursive depth-first traversal using an explicit stack and compare the order of processing.

30

Concept support

Design a recursion assignment

Create a multi-part assignment that asks students to trace, implement, test, and analyze one recursive problem.

Need support with Unit 1?

Work through the concepts, code, edge cases, and runtime analysis with 1:1 guidance.