SMALLTALK LAB
Sparse tables in Squeak: answer static range minima
Precompute windows of power-of-two lengths, then answer a range minimum with two overlapping windows.
Before you begin
Use native Squeak 6.1. The sample readings are fixed; queries use one-based, inclusive endpoints. Start with the eight readings shown, and keep the original array separate from the table.
These are native Squeak 6.1 examples. Each checkpoint is independent: open the download as text, copy one complete expression into Workspace and choose Print it. Do not file in the checkpoint collection as a class definition.
Expected values are Smalltalk expressions. The printer may display equivalent Smalltalk literals with different spaces or delimiters.
01 - Store the length-one row
The minimum of a one-element window is that element. A copy keeps the first row independent from the original list.
| data |
data := #(9 4 7 3 8 2 6 5).
data copyExpected: #(9 4 7 3 8 2 6 5)

Try it: Which reading is the smallest? It is 2.
02 - Join adjacent halves
Combine adjacent singletons to compute each length-two window. Its left coordinate still identifies the result.
| data |
data := #(9 4 7 3 8 2 6 5).
((1 to: data size - 1) collect: [:i |
(data at: i) min: (data at: i + 1)]) asArrayExpected: #(4 4 3 3 2 2 5)

Try it: Why is the row one slot shorter? The last item has no following item.
03 - Build every power-of-two row
Build each width 2h from two width-h windows starting h positions apart. Here the widths are 1, 2, 4 and 8.
| build |
build := [:data | | rows half prev |
data isEmpty ifTrue: [Error signal: 'Need at least one value'].
rows := OrderedCollection with: data asArray copy.
half := 1.
[half * 2 <= data size] whileTrue: [
prev := rows last.
rows add: ((1 to: data size - (2 * half) + 1)
collect: [:i | (prev at: i) min: (prev at: i + half)]) asArray.
half := half * 2].
rows asArray].
build value: #(9 4 7 3 8 2 6 5)Expected: #((9 4 7 3 8 2 6 5) (4 4 3 3 2 2 5) (3 3 2 2 2) (2))

Try it: The length-four row at position 2 represents positions 2 through 5.
04 - Precompute the width and level lookup
For lengths 1–8, cache the largest power of two that fits and its row number. This gives constant-time lookup without floating logarithms.
| powers levels |
powers := Array new: 8 withAll: 1.
levels := Array new: 8 withAll: 1.
2 to: 8 do: [:i | | half |
half := i // 2.
powers at: i put: (powers at: half) * 2.
levels at: i put: (levels at: half) + 1].
{powers. levels}Expected: #((1 2 2 4 4 4 4 8) (1 2 2 3 3 3 3 4))

Try it: Length 6 uses width 4 in table row 3. For inputs of other lengths, build lookups through that length.
05 - Cover the range with overlapping windows
For positions 2–6, take the width-four windows 2–5 and 3–6. The overlap is harmless because repeating a value does not change a minimum.
| build powers levels query |
build := [:data | | rows half prev |
data isEmpty ifTrue: [Error signal: 'Need at least one value'].
rows := OrderedCollection with: data asArray copy.
half := 1.
[half * 2 <= data size] whileTrue: [
prev := rows last.
rows add: ((1 to: data size - (2 * half) + 1)
collect: [:i | (prev at: i) min: (prev at: i + half)]) asArray.
half := half * 2].
rows asArray].
powers := Array new: 8 withAll: 1.
levels := Array new: 8 withAll: 1.
2 to: 8 do: [:i | | half |
half := i // 2.
powers at: i put: (powers at: half) * 2.
levels at: i put: (levels at: half) + 1].
query := [:table :left :right | | len width row |
((left < 1 or: [right > table first size]) or: [left > right])
ifTrue: [Error signal: 'Invalid inclusive range'].
len := right - left + 1.
width := powers at: len.
row := table at: (levels at: len).
(row at: left) min: (row at: right - width + 1)].
query value: (build value: #(9 4 7 3 8 2 6 5)) value: 2 value: 6Expected: 2

Try it: The windows have minima 3 and 2. Their minimum is 2.
06 - Check singleton and full span
The windows coincide on exact powers of two, including a singleton. The same code intentionally takes both minima.
| build powers levels query |
build := [:data | | rows half prev |
data isEmpty ifTrue: [Error signal: 'Need at least one value'].
rows := OrderedCollection with: data asArray copy.
half := 1.
[half * 2 <= data size] whileTrue: [
prev := rows last.
rows add: ((1 to: data size - (2 * half) + 1)
collect: [:i | (prev at: i) min: (prev at: i + half)]) asArray.
half := half * 2].
rows asArray].
powers := Array new: 8 withAll: 1.
levels := Array new: 8 withAll: 1.
2 to: 8 do: [:i | | half |
half := i // 2.
powers at: i put: (powers at: half) * 2.
levels at: i put: (levels at: half) + 1].
query := [:table :left :right | | len width row |
((left < 1 or: [right > table first size]) or: [left > right])
ifTrue: [Error signal: 'Invalid inclusive range'].
len := right - left + 1.
width := powers at: len.
row := table at: (levels at: len).
(row at: left) min: (row at: right - width + 1)].
{query value: (build value: #(9 4 7 3 8 2 6 5)) value: 4 value: 4.
query value: (build value: #(9 4 7 3 8 2 6 5)) value: 1 value: 8}Expected: #(3 2)

Try it: What would the singleton at position 8 return? 5.
07 - Check every span against a direct scan
Scan each of the 36 nonempty spans independently and compare it with the table result.
| build powers levels query readings grid checks |
build := [:data | | rows half prev |
data isEmpty ifTrue: [Error signal: 'Need at least one value'].
rows := OrderedCollection with: data asArray copy.
half := 1.
[half * 2 <= data size] whileTrue: [
prev := rows last.
rows add: ((1 to: data size - (2 * half) + 1)
collect: [:i | (prev at: i) min: (prev at: i + half)]) asArray.
half := half * 2].
rows asArray].
powers := Array new: 8 withAll: 1.
levels := Array new: 8 withAll: 1.
2 to: 8 do: [:i | | half |
half := i // 2.
powers at: i put: (powers at: half) * 2.
levels at: i put: (levels at: half) + 1].
query := [:table :left :right | | len width row |
((left < 1 or: [right > table first size]) or: [left > right])
ifTrue: [Error signal: 'Invalid inclusive range'].
len := right - left + 1.
width := powers at: len.
row := table at: (levels at: len).
(row at: left) min: (row at: right - width + 1)].
readings := #(9 4 7 3 8 2 6 5). grid := build value: readings. checks := 0.
1 to: 8 do: [:left | left to: 8 do: [:right |
(query value: grid value: left value: right) = (readings copyFrom: left to: right) min
ifFalse: [Error signal: 'Reference scan disagrees'].
checks := checks + 1]].
checksExpected: 36

Try it: This tests every span of the sample, not every possible input.
Replay the checkpoints

Reason about correctness and limits
The table answers queries about a fixed array snapshot. If the readings change often, use a segment tree or rebuild the table.
Overlap works for minimum because min(x,x) = x. It would double-count items for sum; use prefix sums for static sums.
Table construction uses O(n log n) time and space. The auxiliary width and level arrays take O(n) space and time; each checked query then uses O(1) array lookups.
Three practice challenges
Trace a new range
Find the minimum on positions 3–5 and identify both windows.
Show solution
Length 3 uses windows 3–4 and 4–5. Both have minimum 3.
Predict an overlap pitfall
What would happen if the range operation were sum?
Show solution
Positions in both windows would be counted twice, so the method would be incorrect.
Reject an empty range
Pass left 4 and right 3.
Show solution
The function raises Invalid inclusive range before looking up a width.
Reference
Bender and Farach-Colton: The LCA Problem Revisited, §3 A Faster RMQ Algorithm
Continue learning
Segment Tree | Prefix Sums | First Min Max
Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.