Implement an in-memory file store that supports writing a new file and reading an existing file while sharing stored content between paths whose data is exactly identical.
For this practice version, process a sequence of operations through one callable. Each operation is either ["write", path, data] or ["read", path]. Return the data produced by every read, in operation order.
Different paths are distinct logical files. Files with identical data must refer to one retained copy of that content rather than each storing a separate retained copy. Files whose data differs must remain distinguishable, including when they have the same length.
Input
-
operations
: an array of string arrays describing writes and reads in their execution order.
-
A write contains exactly three strings:
"write"
, a path, and its data.
-
A read contains exactly two strings:
"read"
and a path.
Output
Return an array containing the data from each read operation, in order. If there are no reads, return an empty array.
Constraints and Edge Cases
-
For this practice version, there are between
1
and
100000
operations.
-
Paths are nonempty ASCII strings and are treated as exact, case-sensitive keys. No path normalization or directory operations are required.
-
Data contains ASCII characters and may be empty.
-
Every write uses a path that has not previously been written. Every read refers to a previously written path.
-
Operations contain no overwrites or deletions.
-
The sum of data lengths across writes and the sum of data lengths across returned reads are each at most
1000000
characters.
-
Exact content equality determines sharing; equal lengths or equal hash values alone do not establish that content is equal.
-
Perform the operations in memory. Do not access the host file system.
Example 1
operations = [
["write", "/a", "hello"],
["write", "/b", "hello"],
["read", "/a"],
["read", "/b"],
["write", "/c", "world"],
["read", "/c"]
]
output = ["hello", "hello", "world"]
The first two paths share their retained content. The third contains different data despite having the same data length.
Example 2
operations = [
["write", "/empty", ""],
["write", "/upper", "HELLO"],
["write", "/lower", "hello"],
["read", "/empty"],
["read", "/upper"],
["read", "/lower"]
]
output = ["", "HELLO", "hello"]
Empty data is valid, and content comparison is case-sensitive.