KatInterview

Group Identical Files by Content from an In-Memory Listing

Arrays & HashingAuto-graded · 14 test cases
EasyAuto-gradedArrays & Hashinghashinggroupingstrings

Last updated

Group file entries that have identical byte content, using content length as a cheap first filter, and return only groups of two or more paths in a specified order.

Find Files With Identical Content

You are given a snapshot of a file collection as a list of entries. Each entry is a dictionary with two keys:

  • "path": a string, the file's path.
  • "content": a list of integers in the range 0..255, the file's bytes in order.

Write a function that groups together the paths of files whose contents are exactly equal, and returns only the groups that contain at least two paths.

Rules

  1. Two files are duplicates if and only if their content lists are identical (same length, same values in the same order).
  2. Each group in the result is a list of paths. A file whose content is unique must not appear anywhere in the result, so every group has at least 2 paths.
  3. Empty files (content == []) are duplicates of each other. If two or more exist, they form one group.
  4. Files of different lengths can never be duplicates, so a sensible solution compares lengths first and only compares content among files of equal length.

Output order

The result must be deterministic:

  • Paths inside a group appear in the same relative order as in the input.
  • Groups are ordered first by when their content length was first seen in the input (a length seen earlier comes first). Groups with the same length are ordered by the first appearance of their content in the input.

If no duplicates exist, return an empty list.

Constraints

  • 0 <= len(files) <= 10^4
  • Each path is a non-empty string; all paths are distinct.
  • Each content has length between 0 and 10^4, with every value in 0..255.
  • The total number of bytes across all files is at most about 10^6.

Examples

Example 1

Input: group_identical_files([{'path': '/data/a.txt', 'content': [104, 101, 108, 108, 111]}, {'path': '/data/b.txt', 'content': [104, 101, 108, 108, 111]}, {'path': '/data/c.txt', 'content': [119, 111, 114, 108, 100]}, {'path': '/data/d.txt', 'content': [120]}, {'path': '/data/sub/e.txt', 'content': [104, 101, 108, 108, 111]}])

Output: [['/data/a.txt', '/data/b.txt', '/data/sub/e.txt']]

Example 2

Input: group_identical_files([{'path': '/data/empty-a', 'content': []}, {'path': '/data/empty-b', 'content': []}])

Output: [['/data/empty-a', '/data/empty-b']]

Hints

Hint 1

Two files with different lengths can never match, so start by bucketing entries by the length of their content.

Hint 2

Inside a bucket, convert each content list to something hashable (for example `bytes` or a tuple) and use it as a dictionary key that maps to a list of paths.

Hint 3

Python dictionaries keep insertion order. Using them at both levels gives you the required output order for free. Finally, keep only the groups with at least two paths.