Problem Solving — CS & DS

Lecture 17 — Searching and Linear Scans

Learning objectives

Case-based lecture: you attempt, we discuss, then we reveal.

Today

The plan

Key definitions

Key definitions

Explain each in one breath; the cases will do the heavy lifting.
Worked example

Find the First Freeze: scan with early exit

cs-065

Daily temperatures are 12, 8, 3, -1, -4, 2 degrees. On which day index does the first freeze (temperature at or below 0) occur?

temps = [12, 8, 3, -1, -4, 2]
idx = None
for i, t in enumerate(temps):
    if t <= 0:
        idx = i
        break
print(idx, temps[idx])

Expected: first freeze at index 3 (temperature -1)

Cost: O(n) worst case, but best case O(1) when the freeze is early; early exit is free - take it.

Worked example

The Course Finder: linear search on an unsorted catalogue

cs-066

A catalogue holds (code, title) pairs in no particular order. Find the title for code 'DS2', and define the behaviour for an unknown code.

catalogue = [("CS1", "Problem Solving"), ("DS2", "Statistics"), ("CS3", "Systems")]

def find(code):
    for c, title in catalogue:
        if c == code:
            return title
    return "not offered"

print(find("DS2"), find("XX9"))

Expected: Statistics / not offered

Cost: O(n) time, O(1) space; already optimal for unsorted input.

Worked example

Peak Finding: the first descent marks a peak

cs-068

In the values 1, 3, 8, 4, 2 find a peak - a value at least as large as its neighbours.

a = [1, 3, 8, 4, 2]
peak = None
for i in range(len(a) - 1):
    if a[i] >= a[i + 1]:
        peak = i
        break
if peak is None:
    peak = len(a) - 1
print(a[peak])

Expected: a peak is 8 (at index 2)

Cost: O(n) here; lecture 21 revisits this problem with binary search for O(log n) - a first taste of the same problem having algorithms of different cost.

Case attempt · lecture 17
Case cs-065 · Beginner

Find the First Freeze

Given daily temperatures, find the first day below freezing, or report none. Linear scan with an early exit and a correct 'not found' result.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-065 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-065 — not embedded in this deck.

Case attempt · lecture 17
Case cs-066 · Foundational

The Course Finder

Find all courses taught in a room with capacity at least X and free on Tuesdays. Filtering as a search with a compound predicate.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-066 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-066 — not embedded in this deck.

Case attempt · lecture 17
Case cs-067 · Intermediate

Two Sorted Halves

A list is two sorted runs concatenated. Find the smallest element. Shows how preconditions (knowing structure) change the algorithm.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-067 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-067 — not embedded in this deck.

Case attempt · lecture 17
Case cs-068 · Expert

Peak Finding

Find a local maximum in a list (greater than its neighbours). Comparing neighbours safely at the edges; why 'a peak exists' needs an argument.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-068 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-068 — not embedded in this deck.

Check yourself

Common misconceptions

Summary

Summary & exit question

Exit question (choose one, 60 seconds)

  1. Cost of a full scan vs early exit, in words.
  2. What precondition made cs-067 fast?
  3. Name one search where early exit is wrong.
Expected answers (instructor): always n touches vs stops at the first match; existence searches often exit early the two halves are each sorted — structure enables strategy counting all matches; you must see every item
→ / space: next · ←: back · Home/End: jump · Ctrl+P → PDF