KatInterview

Select Stale Replica Files for Copying

Arrays & HashingAuto-graded · 17 test cases
EasyAuto-gradedArrays & Hashinghash-mapfilteringsimulation

Last 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:

  1. The source file is readable.
  2. 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).

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

  • sources and destinations are lists of dictionaries with the keys described above (either list may be empty).
  • Names in sources are unique, and names in destinations are unique.
  • size >= 0
  • mtime >= 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.