Python Interview Questions
Infra/SRE focuse
Dictionary File Problem
Suppose that you have a dictionary file that has words separated by a single whitespace, kept in sorted order. So for instance, if you had three words in your dictionary, "dog", "cat", and "horse", your dictionary file would look like:
cat dog horse
Implement a function exists that indicates whether an argument passed in dictionary.
def exists(word, filename): """Returns true iff word is part of the dictionary represented by filename"""
2. If you could modify the format of this file to support more efficient lookups, what would you change about the file format?
3. Compound words are words that are composed of other words. Using the exists() function you implemented earlier, implement a function that indicates whether a word is a compound word.
def is_compound(word, filename): """Returns true iff word is a compound word"""
So, for example, if you have the same dictionary as indicated above, the following invocations of is_compound should return the following values:
is_compound('catdog', dictionary_file) -> True is_compound('dogcat', dictionary_file) -> True is_compound('catdoghorse', dictionary_file) => True is_compound('dogparrotcat', dictionary_file) => False
Implement a function exists that indicates whether an argument passed in is part of the dictionary.
Here's an example of a well thought out binary search solution:
2. If you could modify the format of this file to support more efficientlookups, what would you change about the file format?
Some good answers:
Instead of having variable length records, have fixed length records. This
makes it easier to seek instead of having to search through the file.
Store it as a binary lookup tree with fixed length records so that you can do
easy lookups. For instance, instead of cat dog horse, you could have dog cat
horse and determine where to binary jump next based on indicies (see
Maintain a prefix tree at the beginning of the file that lets you determine
if certain prefixes are part of the file, possibly with offsets that let you
jump to the correct places. See also, SSTables.
3. Compound words are words that are composed of other words. Using the exists() function you implemented earlier, implement a function that indicates whether a word is a compound word.
Example solution:
Last updated
Was this helpful?