37A fragment that finds one item
37 A fragment that finds one item
Shortest Uncommon Substring in an Array is on Airbnb's LeetCode company tag lists in two separate compilations: the
all-time list and the six-month list in one (2 sources), and the six-month list in the other
(1 source). An Airbnb software engineer candidate reported getting that question with the input in a different form
(1 report). Two other questions in the first compilation carry the same trie tag without asking this: Word Break on
Netflix's list (1 source) and Palindrome Pairs on Airbnb's (1 source). This is practice in that shape, not a leaked
question or LeetCode's version. LeetCode's version breaks a tie by the fragment that comes first in the alphabet, not
the earliest start, and gives an empty string where this one gives None.
Set a 45-minute timer. Read one level at a time, and run only that level's tests until they pass:
pytest problems/37_shortest_fragment -k level1. Earlier levels must keep passing.
The input, at every level. A toy shop's search box shows every item whose name contains what you type, anywhere in
the name. Next to each item, the shop wants to print the shortest thing to type that shows that item and nothing else.
names is a list of distinct, non-empty names made of the lowercase letters a to z. A prefix of a name is a
run of its letters from the start, and a fragment is a run of side-by-side letters from anywhere in it. Both
include the whole name.
names = ["kite", "kitten", "kayak", "yoyo", "robot", "rock", "rocket"]
Level 1. Write shortest_prefixes(names). It returns a dict from each name to its shortest prefix that no other
name starts with, or None if every prefix of it, the whole name included, starts another name too. Above: kite gives
"kite", because kitten starts with kit. Then kitten gives "kitt", kayak "ka", yoyo "y", robot "rob" and
rocket "rocke". rock gives None, because rocket starts with all of rock.
Level 2. Write shortest_fragments(names). It returns a dict from each name to its shortest fragment that no
other name contains anywhere. When several fragments of that length qualify, the one that starts earliest in the name
wins. A name gets None if no fragment works, which happens exactly when another name contains the whole of it.
Above:
- kite gives
"ite". Every letter of kite and every fragment of two letters is in kitten, and so is kit. - kitten gives
"n", kayak"a"and robot"b": one letter no other name has. - yoyo gives
"yo". It appears twice in yoyo and in no other name. - rocket gives
"ke"."et"is two letters and unique too, but"ke"starts earlier. - rock gives
None, because rocket contains it.
Level 3. The shop's catalog changes. Write the class FragmentIndex(names) with three methods.
add(name)puts a name in the index. It's never called with a name already there.remove(name)takes one out. It's only called with a name in the index.fragment(name)returns the level 2 answer for a name in the index, among the names in the index right now.
Starting from the names above, add("bobcat") changes robot's answer to "ot", since bobcat has b, bo and ob.
Starting from the names above again, remove("rocket") changes rock's answer from None to "c".
Changes and questions come in all day, so fragment must not start over. The tests build an index of 5,000 names of
9 to 12 letters, then run 3,000 rounds of one add, one remove and one fragment, and allow one second for all of
it, building included.
Things to get right: a name that starts or sits inside another name, a fragment that appears twice in one name, a short fragment late in the name against a longer one at its start, two fragments of the same length, and removing a name that has a repeated fragment.
Be ready to talk about what comes next: names thousands of letters long, what a dict per node costs and what a
Counter of fragments would cost instead, removing a name so its memory comes back, and a catalog of a million items.