SECTION 30 / 31

Debug the first occurrence, the repeated key and empty input

1 min read
Before a pronoun search set matched=-1; with no earlier noun, retain -1 and report “no preceding noun”.
Before a pronoun search set matched=-1; with no earlier noun, retain -1 and report “no preceding noun”.

Trace tiny inputs before processing the whole dataset. Use no records, one record, the same group twice, a missing key, a stored zero, and two different records with a shared name. Decide whether the algorithm counts occurrences or unique IDs. Test the key and value types separately.

MistakeEffectFix
d[k]=d[k]+1 before a first occurrenceReads an absent valueCreate 1, or create 0 then increment
d[k]=1 on every occurrenceErases the accumulated countIncrement when present
inner dictionary={} on every rowLoses previous inner keysInitialize only for a new outer key
Assume keys(d) is sortedOrder-dependent result can failSort the key list explicitly when required
Use −1 as a real word positionWrong word accessCheck sentinel before looking up a word
Count names instead of unique student IDsCan overwrite recordsChoose a unique key or group records deliberately

Try it yourself

What tiny input catches overwriting a frequency with 1?

Show explained answer

Two records with the same key. The expected frequency is 2, not 1.

Try it yourself

What catches an unguarded average?

Show explained answer

An empty dictionary, where count is 0.

Try it yourself

What catches a pronoun matcher that forgets its sentinel?

Show explained answer

A pronoun with no preceding noun, such as the first word of the supplied paragraph.

YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑