Problem · Array

Group Duplicate Files by Content

Learn this problem
MediumPostman logoPostmanFULLTIMEONSITE INTERVIEW

Problem statement

You are given parallel arrays paths and contents, where contents[i] is the complete content of the file at paths[i].

Group files whose contents are exactly equal. Return one string for every group containing at least two files. Within a group, sort paths lexicographically and join them with a comma. Sort the returned group strings lexicographically.

A production filesystem implementation may use a content checksum to find candidates, but it must verify exact content equality before declaring duplicates so checksum collisions cannot merge different files.

Function

groupDuplicateFiles(paths: String[], contents: String[]) → String[]

Examples

Example 1

paths = ["/a/x.txt","/b/y.txt","/c/z.txt"]contents = ["hello","hello","bye"]return = ["/a/x.txt,/b/y.txt"]

The first two files have exactly equal contents. The third file is unique.

Example 2

paths = ["b","a","d","c"]contents = ["1","2","1","2"]return = ["a,c","b,d"]

Paths are sorted inside each duplicate group, and the group strings are sorted before returning.

Constraints

  • 1 ≤ paths.length = contents.length ≤ 10^5
  • Every path is unique and non-empty.
  • The total length of all paths and contents is at most 10^6.
  • Paths and contents may contain spaces, but paths do not contain commas.

More Postman problems

drafts saved locally
public String[] groupDuplicateFiles(String[] paths, String[] contents) {
    // write your code here
}
paths["/a/x.txt","/b/y.txt","/c/z.txt"]
contents["hello","hello","bye"]
expected["/a/x.txt", "/b/y.txt"]
checking account