File System Paths
Updated
Problem statement
Every stored path in this file system holds a whole number. A path is a chain of names, each with a / in front, such as /reports or /reports/summer/week. Dropping the last name gives the parent: /reports/summer is the parent of /reports/summer/week, while a one-name path like /reports has no parent. Names count as whole words, so /card is not the parent of /cards/gold.
commands holds two kinds of strings:
"CREATE path value": ifpathis new and its parent is stored (or it has no parent), savevaluethere and add1to the answer. Otherwise add0and store nothing. A missing parent is never made for you, and a path that is already stored keeps its first value."GET path": add the value saved at exactlypath, or-1if nothing is saved there.
The file system is empty when commands begins. Return the numbers added by the commands, one per command, in the order the commands appear.
Function
runCommands(commands: String[]) → int[]Examples
Example 1
commands = ["CREATE /photos 7","CREATE /photos/trips 41","CREATE /photos 99","GET /photos","GET /photos/trips/june"]return = [1,1,0,7,-1]/photos has one name, so it needs no parent and is stored with 7. /photos/trips is stored because its parent /photos exists. The second CREATE /photos gives 0 because the path is already stored, and the value stays 7, as the GET shows. Nothing was ever stored at /photos/trips/june, so its GET gives -1.
Example 2
commands = ["CREATE /bank/loans/auto 300","CREATE /bank 5","CREATE /bank/loans/auto 300","GET /bank/loans/auto","CREATE /bank/loans 8","CREATE /bank/loans/auto 300","GET /bank/loans/auto"]return = [0,1,0,-1,1,1,300]At first nothing is stored, so creating /bank/loans/auto fails. After /bank is stored, the same command still gives 0: its parent is /bank/loans, and an older ancestor does not count. Neither failure stored anything, so the first GET gives -1. Once /bank/loans exists, the third try succeeds and the last GET gives 300.
Example 3
commands = ["CREATE /card 2","CREATE /cards/gold 6","GET /cards","CREATE /card/gold 9","CREATE /card/gold 9","GET /card/gold"]return = [1,0,-1,1,0,9]Paths are compared name by name, not letter by letter. /card is not the parent of /cards/gold, because cards and card are different names, so that command gives 0 and GET /cards gives -1. /card/gold does have /card as its parent and is stored with 9. Repeating that exact command gives 0, even with the same value.
Constraints
1 <= commands.length <= 2000- Each command is
CREATE path valueorGET path, with one space between parts. - Every path is 2 to 60 characters long. It is one or more names, each with a single
/in front, and each name is one or more lowercase letters fromatoz. A path never ends with/. - Each
valueis an integer in the range1to10^9, and its first digit is not0.
More Capital One Onsite Interview questions
- Digit-Coded Card ValidationONSITE INTERVIEW · Seen Nov 2026
- Sort ColorsONSITE INTERVIEW · Seen Oct 2026
- Virtual Card Transaction ValidatorONSITE INTERVIEW · Seen Oct 2026
- Banking System with Top ActivityONSITE INTERVIEW · Seen Sep 2026
- Transaction Event StoreONSITE INTERVIEW · Seen Jul 2026
- Shortest Path in an Unweighted GraphONSITE INTERVIEW · Seen Jul 2026
- Duplicate Transactions Within 60 SecondsONSITE INTERVIEW · Seen May 2026
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen May 2026
- Cashback TrackerONSITE INTERVIEW · Seen Nov 2025
- Reverse a StringOA · ONSITE INTERVIEW · Seen Oct 2025
Similar questions at other companies
- In-Memory Directory and File OperationsSquarepoint Capital · ONSITE INTERVIEWSquarepoint Capital interview questions
- Design File SystemGoogle · PHONE SCREENGoogle interview questions
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- In-Memory Vault File SystemHarvey · ONSITE INTERVIEWHarvey interview questions
- Word Dictionary with Wildcard FindAmazon · ONSITE INTERVIEW
- Transform Dotted Keys into a Nested MapProofpoint · ONSITE INTERVIEW
- Longest-Match Tokenizer with Unknown SymbolsAnthropic · ONSITE INTERVIEWAnthropic interview questions
- Implement uniq: Unique LinesVanta · ONSITE INTERVIEW
- Implement a TrieMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Text Embedding and Vector SearchNike, Inc. · PHONE SCREEN · ONSITE INTERVIEWNike, Inc. interview questions
Capital One interview experiences
- Capital One Experienced Engineer Power Day and OA: 6 Experiences
- Capital One New Grad OA Questions: 14 Experiences (Part 2 of 2)
- Capital One TDP Agentic Round (AI Coding): 7 Experiences
- Capital One SWE Power Day, Level Not Reported: 6 Experiences
- Capital One TIP Intern OA and Power Day: 13 Experiences
- Capital One TDP Agentic Power Day: 25 Experiences (Part 1 of 2)