Design File System

MediumDesigntrie

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Design a store that maps **paths** to integer values. A path is one or more segments of lowercase letters, each preceded by /, such as /app or /app/config. - FileSystem() creates an empty store. - createPath(path, value) creates path with value and returns true. It returns false and changes nothing if path already exists, or if its **parent path** (everything before the last /) does not exist. A path with a single segment has the root as its parent, which always exists. - get(path) returns the value stored at path, or -1 if the path does not exist. Existing paths are never overwritten. Segments are compared as whole strings, so /ab and /a/b are unrelated.

Examples

Input: ["FileSystem","createPath","get","createPath","get","createPath","get"] [[],["/app",4],["/app"],["/app/config",9],["/app/config"],["/app",5],["/app"]]
Output: [null,true,4,true,9,false,4]
`/app/config` can be created because `/app` exists. Creating `/app` a second time fails and keeps value 4.
Input: ["FileSystem","createPath","get","createPath","createPath","get"] [[],["/x/y",2],["/x"],["/x",1],["/x/y",2],["/x/y"]]
Output: [null,false,-1,true,true,2]
`/x/y` cannot be created before its parent `/x` exists. Once `/x` is created, it succeeds.

Constraints

  • `2 <= path.length <= 100`
  • Every path is well-formed: it starts with `/`, has no empty segments or trailing `/`, and segments use only lowercase letters.
  • `1 <= value <= 10^9`
  • At most 10^4 calls in total.

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More trie problems