R

E

A

C

T

O

xamples 

epeat

ode

pproach

ptimize

est

{Prefix Search}

The Question

Given:

  • a "book" object which contains
    • a numerical 'id' property and
    • a string 'text' property
  • a string to search for in the 'text'

 

Return:

  • An array with the character index for every word that begins with that string.  The search should be case insensitive.

 

Example

What output would you expect?

One Naive Approach

What is the bigO in time complexity?

Another Naive Approach

 

What is the most appropriate data structure for this problem?

Tries

(generally pronounced 'trees')

What do you remember about them?

Tries

(generally pronounced 'trees')

  • Also called:
    • Digital tree
    • Radix tree
    • Prefix tree
  • Used for re(trie)val
  • Essentially a tree for storing strings
  • Root associated with an empty string
  • Each node is a character that points to the next
  • All the descendants of a node have a common prefix of the string associated with that node

Tries

What words are stored in this trie?

Tries

(generally pronounced 'trees')

  • You can use sets or hash tables to search, but tries really shine when looking for words that have a single character different or a prefix in common, or a character missing
  • Use cases?

 

Tries

(generally pronounced 'trees')

Interviewer Tips

 

  • Naive coding first is fine!
  • You can let them know the 'id' is not really important for a naive coding if they seem confused about why it exists
  • Work on teaching them the concepts of tries and the build method
  • Encourage drawing out the data structure
  • Don't need to return for this review at 10:15 -- same info will be presented

 

More Trie stuff:

Explanations and extra problems

 

  • https://en.wikipedia.org/wiki/Trie
    • Comparisons to BSTs and hash tables
  • https://www.topcoder.com/community/data-science/data-science-tutorials/using-tries/
    • Good explanations and extra practice problems

Prefix Search

By mschreiber

Prefix Search

Technical interview problem on finding the character index for every word that begins with a given string.

  • 1,025