def autocomplete(corpus, prefix):
    """
    Given a text corpus and a prefix, return a list
    of all words that start with that prefix
    """
    class Trie:
        def __init__(self, value):
            self.value = value
            self.children = {}
            # allows for quick access of resulting words,
            # rather than having cumbersome traversal
            self.words = []

    if not len(prefix):
        return corpus

    # construct trie
    root = Trie('')
    for word in corpus:
        cur = root
        cur.words.append(word)
        for char in word:
            if char not in cur.children:
                cur.children[char] = Trie(char)
            cur = cur.children[char]
            cur.words.append(word)

    for char in prefix:
        if char in root.children:
            root = root.children[char]
        else:
            return []

    return root.words