Quick Overview

Identify source files requiring synchronization by parsing metadata, checking source readability, destination presence and writability, and size or modification-time differences while preserving source order.

Identify Files That Need Synchronization

Company: Jump Trading

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Online Assessment

## Problem Compare source and destination file listings and return source filenames that should be synchronized, preserving source-list order. Each listing row has five space-delimited fields: permissions, username, size, modification time, and filename. A source file is eligible only when the source is readable. It needs syncing when it is absent at the destination, or when size or modification time differs and the existing destination is writable. ### Function Contract Implement `files_to_sync(source_files, destination_files) -> list[str]`. Permission strings have three characters in read/write/execute order, with `-` for a missing permission. ### Constraints - Each list contains at most 200000 rows, and filenames are unique within a list. - Usernames and filenames contain no spaces; size is a nonnegative integer; time is `HH:MM:SS`. - Readable means source permissions begin with `r`; writable means destination permissions have `w` as the second character. - If size and modification time both match, do not sync even when other fields differ. ### Examples For this input: ```text source_files = [ "rwx alex 2673422 05:04:03 somefilename.txt", "rwx alice 993432 05:45:00 myfile.csv", "rwx alex 734673422 19:24:13 foobar.txt", "-wx root 184 12:04:05 barfoo.txt", "r-- alice 10 02:00:00 helloworld.txt" ] destination_files = [ "rwx alex 5 05:04:03 somefilename.txt", "rwx alice 993432 10:45:00 myfile.csv", "r-x alex 734673422 04:24:13 foobar.txt", "rwx root 4 12:04:05 barfoo.txt" ] ``` return `["somefilename.txt","myfile.csv","helloworld.txt"]`. The first file has a different size and a writable destination; the second has a different modification time and a writable destination; the third changed but its destination is not writable; the fourth source is not readable; and the fifth is readable and missing at the destination. - A changed source row beginning with `-wx` is skipped because it is not readable. ```hint Index destination by filename Parse each destination row once so every source comparison is constant expected time. ``` ```hint Apply permissions before differences A missing destination needs no write-permission check, while an existing destination must be writable before replacement. ``` ### Edge Cases - A destination-only file is ignored. - A missing source-readable file is returned regardless of its write bit. - A malformed row is outside the guaranteed input contract.

Overview: Identify source files requiring synchronization by parsing metadata, checking source readability, destination presence and writability, and size or modification-time differences while preserving source order.

Compare source and destination file listings and return source filenames that should be synchronized in source-list order. Every row contains permissions, username, size, modification time, and filename as five space-delimited fields. A source is eligible only when its permissions begin with r. A readable source needs syncing when absent at the destination, or when size or modification time differs and the existing destination permissions have w as their second character. Matching size and time never require sync.

Constraints

  • Each list contains at most 200000 valid five-field rows.
  • Filenames are unique within each list and contain no spaces.
  • Permission strings have three read/write/execute positions with - for absence.
  • Sizes are nonnegative integers and times use HH:MM:SS.
  • A readable source permission begins with r.
  • An existing destination is writable only when its second permission character is w.

Examples

Input: (['rwx alex 2673422 05:04:03 somefilename.txt', 'rwx alice 993432 05:45:00 myfile.csv', 'rwx alex 734673422 19:24:13 foobar.txt', '-wx root 184 12:04:05 barfoo.txt', 'r-- alice 10 02:00:00 helloworld.txt'], ['rwx alex 5 05:04:03 somefilename.txt', 'rwx alice 993432 10:45:00 myfile.csv', 'r-x alex 734673422 04:24:13 foobar.txt', 'rwx root 4 12:04:05 barfoo.txt'])

Expected Output: ['somefilename.txt', 'myfile.csv', 'helloworld.txt']

Explanation: Changed writable destinations and a readable missing source are returned in source order.

Input: (['r-- u 1 00:00:00 a.txt'], [])

Expected Output: ['a.txt']

Explanation: A readable source missing at the destination is eligible regardless of its own write bit.

Hints

  1. Index destination metadata by filename before scanning sources.
  2. A missing destination needs no write check, but an existing changed destination does.

Loading coding console...