Common Words Across Files
Updated
Problem statement
Given matching arrays fileNames and contents, find every word that appears in at least two distinct files and identify all files containing it. The string contents[i] is the complete text of the file named fileNames[i].
Split each file’s text at ASCII whitespace: space, horizontal tab, line feed, vertical tab, form feed, or carriage return. Each complete, nonempty piece is a word. Words are case-sensitive, and punctuation remains part of a word. Repeating a word within one file counts that file only once.
Return a two-dimensional string array. Each row contains the word followed by the names of all files containing that word. Sort rows by word in ascending ASCII lexicographic order, and sort the file names within each row in the same order. Return an empty array if no word occurs in at least two distinct files.
Input
The task finds common words together with the files they belong to. This version receives each file's text directly; opening files and handling filesystem errors are not part of your function.
Function
commonWordsAcrossFiles(fileNames: String[], contents: String[]) → String[][]Examples
Example 1
fileNames = ["b.txt","a.txt","c.txt"]contents = ["cat dog cat","cat bird","dog fish"]return = [["cat","a.txt","b.txt"],["dog","b.txt","c.txt"]]cat appears in a.txt and b.txt; its repeated occurrence in b.txt adds no extra membership. dog appears in b.txt and c.txt. Neither word needs to appear in all three files.
Example 2
fileNames = ["left","right"]contents = ["Cat cat, cat","cat, Cat"]return = [["Cat","left","right"],["cat,","left","right"]]Cat and cat, occur in both files. The different token cat occurs only in left. Uppercase C sorts before lowercase c.
Example 3
fileNames = ["solo"]contents = ["echo echo echo"]return = []Only one distinct file contains echo, so repetition alone does not make it a common word.
Example 4
fileNames = ["z","a","m"]contents = ["red\tblue\n","blue\rred"," \f "]return = [["blue","a","z"],["red","a","z"]]Tabs, line feeds, and carriage returns separate tokens. File m contains only whitespace and contributes no words.
Constraints
0 <= fileNames.length = contents.length <= 1000.- File names are unique. Each has length from
1to80and contains only ASCII letters, digits,_,-,., or/. - The sum of all
contents[i].lengthvalues is at most10^5. - File contents contain only printable ASCII characters or the six ASCII whitespace characters listed above. Each word has length at most
80. - A file may be empty or contain only whitespace.
More Meta Phone Screen questions
- Convert a Sorted Linked List to a Balanced BSTPHONE SCREEN · Seen Nov 2026
- Fuzzy Word Lookup With One Misspelled LetterPHONE SCREEN · Seen Nov 2026
- Multi-Head Attention Forward PassPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Run-Length Encoded Vector Dot ProductPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Mutual-Friend RecommendationsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Time to Buy and Sell StockPHONE SCREEN · Seen Nov 2026
- First Element Greater Than KPHONE SCREEN · Seen Nov 2026
- Count Events in Rolling Minute WindowsPHONE SCREEN · Seen Oct 2026
- Valid Word AbbreviationPHONE SCREEN · Seen Oct 2026
- Shortest Path in a Binary MatrixPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Insert an Employee into an Organization HierarchyWaymo · PHONE SCREENWaymo interview questions
- Brazilian Receivables Part 3 — Partial ContractsStripe · PHONE SCREENStripe interview questions
- Summarize Server Healthcheck LogsNVIDIA · PHONE SCREENNVIDIA interview questions
- Transform Dotted Keys into a Nested MapProofpoint · ONSITE INTERVIEW
- Serialize and Deserialize a String Key-Value StoreByteDance · ONSITE INTERVIEWByteDance interview questions
- Deep Merge with a Conflict ResolverAutodesk · ONSITE INTERVIEWAutodesk interview questions
- Number of AtomsAmazon · ONSITE INTERVIEW
- Rank Word-Translation Hints from Sentence TranslationsDuolingo · PHONE SCREEN · ONSITE INTERVIEWDuolingo interview questions
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Most Frequent Call Path From Function Trace LogsRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
Meta interview experiences
- Meta PhD SWE Intern AI Coding Phone Screen: 1 Experience
- Meta Business Support Engineer Phone Screen: 1 Experience
- Meta Software Engineer Onsite Questions: 3 Experiences
- Meta AI Native Engineer OA and Onsite: 1 Experience
- Meta AI-Native Coding Interview: 3 Experiences
- Meta Production Engineer Intern Questions: 6 Experiences