cswork / libraryOpen practice workspace

Coding questions · Applied Intuition

Find duplicate files by size

This question evaluates file parsing, data deduplication and grouping skills along with algorithmic efficiency and space-time trade-offs when identifying duplicate files by size rather than by content.

Question

LeetCode 609. Find Duplicate File in System — find duplicate files using file size instead of content comparison

https://leetcode.com/problems/find-duplicate-file-in-system/description/

Find Duplicate Files by Size

You are given a list of directory descriptions similar to LeetCode 609, but instead of grouping files by identical content, you must group them by identical file size.

Each string in the input has the form: "directory file1(size1) file2(size2) ... filek(sizek)"

For every file, build its full path as "directory/filename". Two files are considered duplicates if their sizes are equal.

Return all groups of duplicate files. Only include sizes that appear at least twice.

To make the output deterministic:

  1. File paths inside each group must appear in the same order they are encountered while scanning the input from left to right.
  2. Groups must appear in the order that their size first appears in the input.

Constraints

  • 0 <= len(paths) <= 20000
  • Each directory description has length between 1 and 2000
  • The total number of files across all strings is at most 100000
  • 0 <= size <= 10^9
  • Directory names and file names contain no spaces, and file names do not contain parentheses

Examples

Example 1

Input: (["root/a 1.txt(100) 2.txt(200) 3.txt(100)", "root/c 4.txt(300)", "root/c/d 4.txt(200)", "root 4.txt(300)"],)
Output: [["root/a/1.txt", "root/a/3.txt"], ["root/a/2.txt", "root/c/d/4.txt"], ["root/c/4.txt", "root/4.txt"]]

Explanation: Size 100 appears in root/a/1.txt and root/a/3.txt, size 200 appears in root/a/2.txt and root/c/d/4.txt, and size 300 appears in root/c/4.txt and root/4.txt.

Example 2

Input: (["home 1.txt(10) 2.txt(20)", "var 3.log(30)"],)
Output: []

Explanation: Every file size is unique, so there are no duplicate groups.

Hints

  1. Use a hash map where the key is the file size and the value is the list of full paths with that size.
  2. You do not need to compare every pair of files. Parse each file once, build its full path, and append it to the correct group.