Reading Pseudocode
Assignment, iteration, recursion, base cases — translating pseudocode to plain English and back
Pseudocode vocabulary
- ← (arrow) — assignment; for each item in list — iteration; while cond — loop
- Base case — the stop condition in recursion (e.g., if n = 0: return 1)
- Recursive case — the function calls itself with a smaller input
- Running maximum/minimum/sum — a variable that accumulates across iterations
- DFS = "visit node, then recursively visit neighbors"; BFS = "visit all neighbors first (queue)"
Question 0 of 5
Read this pseudocode and choose the best plain-English description:function findMax(list):
max ← list[0]
for each item in list:
if item > max:
max ← item
return max
←— assignment (equivalent to=in code)for each item in list— iteration over every elementif condition:— conditional check- "tracking" or "accumulating" — a variable that updates as the loop runs
This pseudocode uses recursion. What does it compute?function factorial(n):
if n = 0:
return 1
return n * factorial(n - 1)
- Base case — the condition that stops the recursion:
if n = 0: return 1 - Recursive case — the function calls itself with a smaller input:
factorial(n-1) - Call stack — each recursive call is pushed onto the stack; it unwinds as base cases are reached
What does this pseudocode describe?function binarySearch(arr, target, low, high):
if low > high: return -1
mid ← (low + high) / 2
if arr[mid] = target: return mid
if arr[mid] < target: return binarySearch(arr, target, mid+1, high)
return binarySearch(arr, target, low, mid-1)
- low, high — boundaries of the current search range (indices)
- mid — the midpoint index; computed fresh each call
- Base case: low > high — search range is empty, target not found → return -1
- Recursive calls: search the right half (
mid+1, high) or left half (low, mid-1) depending on comparison
Convert this pseudocode description to the matching pseudocode structure:
"Repeatedly remove the smallest element from the input list and append it to the result until the input is empty."
- while condition: ... — repeat while condition holds
- findMinimum — a helper operation (common pseudocode shorthand)
- append to result — build up a new list
- "Repeatedly remove smallest" → selection sort
- "Divide in half, sort each half, merge" → merge sort (option D)
- "Compare adjacent, swap if wrong order" → bubble sort
A design doc describes an algorithm as: "For each node, visit it, then recursively visit all unvisited neighbors." Which algorithm does this describe?
- DFS — explores as deep as possible down one path before backtracking; uses recursion or a stack; "go deep first"
- BFS — explores all neighbors at the current level before going deeper; uses a queue; "go wide first"
- visited set — tracks which nodes have been seen to avoid infinite loops in cyclic graphs