Skip to content

String hashing

storing

Level 1 of 3

Also called Horner's rule and Polynomial string hash.

test yourself

Six questions, four to pick from and two for paper.

fresh numbers: the questions a run answers, asked again on new numbers

choose one

question 1

Which slot does the method give the word ten?

ten#×31+#h0
the answer, and why

the answer is B

t is 20: 0 times 31 plus 20 is 20, which leaves 7. Then 7 times 31 plus 5 is 222, which leaves 1. Then 1 times 31 plus 14 is 45, which leaves 6.

if you chose another

  • That adds the letters, 39, and keeps the remainder. The method multiplies by 31 before each letter.
  • That is the remainder of n's 14 after dividing by 13, the last letter alone. Multiplying by 31 keeps every letter in the value.
  • That reads the letters from the last, n first, as net. The method starts at the first letter.

question 2

How many letters does the method read on the word listen?

listen#×31+#h0
the answer, and why

the answer is C

One letter read each time round the loop, and listen has 6.

if you chose another

  • That is 3 for each letter, a multiply, an add and a remainder. Each letter is read once.
  • That is the watch page's steps, 3 a letter, a start and an end.
  • The slot is opened once, but working it out reads every letter.

question 3

In a word of 2 letters, a and n swap places, an to na. What happens to the slot?

the answer, and why

the answer is B

an is 1 times 31 plus 14, 45. na is 14 times 31 plus 1, 435. 435 less 45 is 390, 30 thirteens, so both leave 6, as question 2 of the learn page shows.

this tests where the formula comes from, on the learn page

if you chose another

  • Usually it would. But na is 435 and an is 45, 390 apart, which is 30 thirteens. So both totals leave the same remainder.
  • Both leave 6.
  • A slot is a remainder, from 0 to 12; it never grows past 12.

question 4

A key grows from 1,000 letters to 2,000. What happens to the letters read to work out its slot?

the answer, and why

the answer is C

By question 1, LL is nn. 2,000 letters read against 1,000.

this tests where the formula comes from, on the learn page

if you chose another

  • The slot is one place, but working it out reads every letter.
  • Each new letter is read too, 1,000 more.
  • That would be every letter read once for each letter. 1,000 × 1,000 is 1,000,000 reads, and 2,000 × 2,000 is 4,000,000, four times as many. Each letter is read once.

pen and paper

question 5about 5 minutes

Work out the slot of fig by hand, with base 31 and 13 slots.

  1. Each letter's number.
  2. The value after each letter.
  3. The slot.
a hint

f is the 6th letter of the alphabet.

a worked answer

f is 6, i is 9 and g is 7. After f: 6, which leaves 6. After i: 6 times 31 plus 9 is 195, 15 thirteens, which leaves 0. After g: 0 times 31 plus 7 is 7, which leaves 7. So fig goes in slot 7.

this tests how it works, on the learn page

what a full answer includes
  • 6, 9 and 7.
  • 6, then 195 leaving 0, then 7.
  • Slot 7.

question 6about 4 minutes

One table is keyed by email addresses of 60 characters, another by documents of 4,096 characters. Each look up opens one slot.

  1. How many characters does one look up read to find its slot in each?
  2. About how many times as long does the second take?
a hint

Use question 1 of the learn page.

a worked answer

By question 1, a method like this one reads each character once: 60 for an email address and 4,096 for a document. 4,096 over 60 is about 68, so about 68 times as long, though each opens one slot. The books' volume two, chapter 16 makes this point.

Question 1: how many letters, LL, does the method read on a word of nn letters?
  1. the move: one letter each time round

    the line:
    each time round, the loopreads the next letter, once\begin{gathered} \text{each time round, the loop} \\ \text{reads the next letter, once} \end{gathered}
    time roundletter readletters read so far
    1c1
    2a2
    3t3
    cat: 3 times round, 3 letters read.

    why it is allowed: The loop starts at the first letter and moves on one letter each time round, until the last. It never goes back.

  2. the move: count them

    the line:
    L=nL = n

    why it is allowed: By line 1, one letter read for each letter of the word. For cat, 3.

  3. the move: a ceiling

    the line:
    L≤nL \le n

    why it is allowed: By line 2, LL is nn, so at most nn.

  4. the move: read the bound

    the line:
    L∈O(n)L \in O(n)

    why it is allowed: By line 3: at most 1 times nn, at every nn.

  5. the move: a floor

    the line:
    L≥nL \ge n

    why it is allowed: By line 2 again, LL is nn, so at least nn.

  6. the move: read the bound

    the line:
    L∈Ω(n)L \in \Omega(n)

    why it is allowed: By line 5: at least 1 times nn, at every nn.

  7. the move: both together

    the line:
    L∈Θ(n)L \in \Theta(n)

    why it is allowed: Lines 4 and 6 bound LL by the same growth, nn, from above and below.

try it with a small nn: at n=3n = 3, line 2 gives L=3L = 3, which you can work out by hand

this tests where the formula comes from, on the learn page

what a full answer includes
  • 60 and 4,096.
  • About 68 times.