← Questions

Shell tab completion for file paths

0
FigmaCodingLast asked May 2022

A coding interview question asked in Figma's coding round, last asked May 2022. It tests strings, trie and paths.

Implement what a shell does when you press Tab in a path. You get the absolute paths of all files, the current directory, and the text the user has typed so far. Return the completed path.

Resolve the typed text first: if it starts with / it is already absolute, otherwise it is relative to the current directory. Then find every file whose path starts with that prefix. If there is at least one, return the longest prefix that all of them share; this can run through one or more directory names. If no file matches, return the resolved prefix unchanged.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • complete(paths: list[str], cwd: str, typed: str) -> str returns the completed absolute path.

Example

paths = ["/home/foo/f1.txt", "/home/foo/f1_2.txt", "/home/foo/README",
         "/home/foo/bar/f1.txt", "/home/foo/bar/README"]
complete(paths, "/home/foo", "f")    -> "/home/foo/f1"
# two files match; they agree through "f1" and then differ ("." and "_")

paths = ["/work/folder/file1", "/work/folder/file2",
         "/work/folder/file/new", "/work/other.txt"]
complete(paths, "/work", "f")        -> "/work/folder/file"
# every match is under folder/, so completion enters it and keeps going

paths = ["/work/folder/file1", "/work/folder/file2", "/work/folder/image.png"]
complete(paths, "/tmp", "/work/folder/fi")  -> "/work/folder/file"
# absolute input ignores the current directory

complete(["/work/a.txt"], "/work", "z")     -> "/work/z"
# no match: the resolved prefix comes back unchanged

Constraints

  • Up to 200,000 paths, with total length up to 10^6 characters.
  • All paths and cwd are absolute and normalized: no repeated /, no . or .. parts. cwd has no trailing / unless it is / itself.
  • typed has no . or .. parts.

Clarifying questions

  • Is the match on plain characters, so f matches folder/... as well as files named f...? (Yes.)
  • Should completion stop at a directory boundary, or run through several directory names when every match shares them? (It runs through.)
  • What if cwd is /: how do you join it with the typed text without a double slash?
  • Will there be one query, or many queries against the same file list?

What a strong answer covers

  • Correct resolution of relative and absolute input, including cwd = "/".
  • For one query: filter by prefix and take the common prefix of the matches, O(total path length).
  • For many queries: a character trie over the paths with a count of files under each node. Walk down the prefix, then keep descending while the node has a single child.
  • The no-match case and the case where the typed text is already a complete file path.
  • Tests from the examples, plus a single match and an empty typed.

Follow-ups

  • Answer many queries fast on a large file tree, and update the trie when files are added or deleted.
  • On a second Tab with several matches, list the candidates' next path parts, as a real shell does.
  • Build the trie on path parts instead of characters, and say what that changes.

Where it's asked

  • Figma · Coding · Last asked May 202260 min · One or two pair-programming rounds against provided tests

What it tests

strings · trie · paths

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding