Word Collection: Distance and Anagrams
Updated
Problem statement
Construct an immutable WordCollection from the ordered list words. Keep repeated words at their original indices. Implement these two methods:
shortestDistance(a,b): return the minimum absolute difference between an index containingaand an index containingb. The two indices must be distinct, even whena == b. Return-1when no such pair exists.areAnagrams(a,b): return whether the two argument strings have exactly the same character frequencies. This method compares the strings themselves; they need not appear in the collection.
For this exercise, assume words never changes. The callable adapter wordCollectionQueries(words,queries) constructs one collection, processes every query in input order, and returns one integer per query:
["distance",a,b]contributesshortestDistance(a,b).["anagram",a,b]contributes1for true and0for false.
For this exercise, assume matching is case-sensitive over lowercase ASCII words, and distance queries may name words absent from the collection. A distance is an index difference, so adjacent occurrences have distance 1. An empty query list returns an empty result. No query changes the collection.
Function
wordCollectionQueries(words: String[], queries: String[][]) → int[]Examples
Example 1
words = ["practice","makes","perfect","coding","makes"]queries = [["distance","coding","practice"],["distance","makes","coding"],["anagram","listen","silent"],["distance","makes","makes"]]return = [3,1,1,3]coding is at index 3 and practice at 0, giving 3. The nearest makes is at index 4, one step from coding. The strings listen and silent are anagrams. The two makes occurrences are at 1 and 4, giving 3.
Example 2
words = ["a","b","a"]queries = [["distance","a","a"],["distance","b","b"],["distance","a","missing"],["anagram","aab","abb"]]return = [2,-1,-1,0]The two a occurrences are at indices 0 and 2. There is only one b, so it cannot supply two distinct indices. The word missing is absent. The strings aab and abb have different character counts.
Example 3
words = []queries = [["anagram","loop","pool"],["anagram","ab","abc"],["distance","loop","pool"]]return = [1,0,-1]Anagram checks do not depend on collection membership: loop and pool have the same multiset; ab and abc have different lengths. The empty collection has no distance pair.
Constraints
- For this exercise, assume
0 <= words.length <= 1000and0 <= queries.length <= 1000. - Each entry of
wordsand each query argument has between1and40lowercase ASCII letters. - Every query contains exactly three strings: its operation and two arguments.
- The only operations are
distanceandanagram.
More Google Onsite Interview questions
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
- Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Filesystem Entity Total SizeONSITE INTERVIEW · Seen Nov 2026
- Longest Subarray Within an Absolute-Difference LimitONSITE INTERVIEW · Seen Nov 2026
- Reach a Destination Through Scheduled FlightsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Shortest Deletion for Distinct CharactersOracle · ONSITE INTERVIEWOracle interview questions
- All Distinct Palindromic SubstringsDoorDash · ONSITE INTERVIEWDoorDash interview questions
- Halfway Course in a Single TrackCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Indexed Proximity SearchStripe · PHONE SCREENStripe interview questions
- Longest Substring Without Repeating CharactersSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Classify Password StrengthHackerRank · PHONE SCREEN · ONSITE INTERVIEWHackerRank interview questions
- Longest Substring Without Repeating CharactersMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Feature Flags with Rules, Dependencies and ExposurePerplexity AI · ONSITE INTERVIEWPerplexity AI interview questions
- Top K Best-Selling Products in the Last N DaysAmazon · ONSITE INTERVIEW
- In-Memory Vault File SystemHarvey · ONSITE INTERVIEWHarvey interview questions
Google interview experiences
- Google L4 SWE Phone Screen: 1 Experience
- Google ML Engineering Manager Onsite: 1 Experience
- Google Experienced SWE First Round and Onsite: 1 Experience
- Google AI Coding Interview and AI OA: 2 Experiences
- Google SWE Intern Interview Results Timeline: 13 Experiences
- Google L4 SWE Phone Screen and Kirkland Onsite: 1 Experience