Select Stale Replica Files for Copying
Arrays & HashingAuto-graded · 17 test casesLast updated
Filter a list of source file records against destination records using readability, writability, size and timestamp rules, returning names in source order.
Replica Copy Planner
A primary machine keeps files that must be mirrored onto a backup machine. Given metadata for both sides, decide which files from the primary need to be copied over.
You receive two lists of dictionaries, sources and destinations.
Each entry in sources has these keys:
name(str): the unique file name or path.size(int): the size in bytes.mtime(int): the last-modified time in Unix seconds.is_readable(bool): whether the file can be read on the primary.
Each entry in destinations has these keys:
name(str): the file name or path.size(int): the size in bytes.mtime(int): the last-modified time in Unix seconds.is_writable(bool): whether the backup copy can be overwritten.
Rule
A source file must be copied if and only if both of the following are true:
- The source file is readable.
- At least one of these is true:
- No destination entry has the same name.
- A destination entry with the same name exists, it is writable, and at least one of these holds:
- the sizes differ (
source.size != dest.size), or - the source is strictly newer (
source.mtime > dest.mtime).
- the sizes differ (
A destination entry that exists but is not writable blocks the copy, so that file is never included. A destination that is newer than the source with the same size does not trigger a copy. Destination entries with no matching source are ignored.
Output
Return a list of the names of all files that need to be copied, in the same order in which they appear in sources.
Constraints
sourcesanddestinationsare lists of dictionaries with the keys described above (either list may be empty).- Names in
sourcesare unique, and names indestinationsare unique. size >= 0mtime >= 0
Examples
Example 1
Input: select_files_to_copy([{'name': 'config.xml', 'size': 500, 'mtime': 1000, 'is_readable': True}, {'name': 'secrets.key', 'size': 128, 'mtime': 1000, 'is_readable': False}, {'name': 'data.csv', 'size': 2048, 'mtime': 1050, 'is_readable': True}, {'name': 'log.txt', 'size': 100, 'mtime': 1000, 'is_readable': True}], [{'name': 'config.xml', 'size': 500, 'mtime': 900, 'is_writable': True}, {'name': 'data.csv', 'size': 2048, 'mtime': 1050, 'is_writable': True}, {'name': 'log.txt', 'size': 100, 'mtime': 1000, 'is_writable': False}])
Output: ['config.xml']
Example 2
Input: select_files_to_copy([{'name': 'a.bin', 'size': 100, 'mtime': 500, 'is_readable': True}, {'name': 'b.bin', 'size': 200, 'mtime': 500, 'is_readable': True}], [{'name': 'a.bin', 'size': 99, 'mtime': 500, 'is_writable': True}])
Output: ['a.bin', 'b.bin']
Hints
Hint 1
Checking every destination for each source works but is slow. Can you index the destination entries by name first?
Hint 2
Handle the readable check first. Then treat 'no destination entry' and 'destination entry exists' as two separate branches.
Hint 3
The time comparison is one-directional: only a source strictly newer than the destination counts. A size mismatch triggers a copy regardless of times, but only when the destination is writable.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Build a dictionary that maps each destination name to its entry, so lookups are constant time on average. Then walk through sources once, in order, and decide for each file:
- If the source is not readable, skip it.
- Look up the destination entry by name.
- If there is none, the file must be created, so add it.
- If there is one, add the file only when the destination is writable and either the sizes differ or the source
mtimeis strictly greater than the destinationmtime.
Why it is correct
The code mirrors the rule directly:
copy(s) = s.is_readable AND (
dest missing
OR (dest.is_writable AND (s.size != dest.size OR s.mtime > dest.mtime))
)
- A non-writable destination only blocks a copy when the destination entry exists. A missing entry has no flag to check, so it is handled in its own branch.
- The time check is strict and one-directional, so equal times or a newer destination with an equal size means the file is up to date.
- A size difference triggers a copy independent of timestamps (provided the destination is writable).
- Iterating over
sourcesin order and appending as we go preserves the required output order. The dictionary is only used for lookups.
Complexity
With n = len(sources) and m = len(destinations):
- Time: O(n + m).
- Space: O(m) for the index, plus up to O(n) for the output.
Reference solution
def select_files_to_copy(sources: list[dict], destinations: list[dict]) -> list[str]:
dest_by_name = {item["name"]: item for item in destinations}
result = []
for src in sources:
if not src["is_readable"]:
continue
dest = dest_by_name.get(src["name"])
if dest is None:
result.append(src["name"])
elif dest["is_writable"] and (
src["size"] != dest["size"] or src["mtime"] > dest["mtime"]
):
result.append(src["name"])
return result