FastPrepWord Collection: Distance and Anagrams
Problems/Word Collection: Distance and Anagrams

Word Collection: Distance and Anagrams

Google logoGoogle● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report gives the phonetic reference 243 and names an anagram class method. Our version is about 75% the same as the reported question for the two named methods. The exact variant and the other methods were not kept. We chose the fixed collection, a finite way to make queries, what happens with equal or missing words, how a yes-or-no answer is written as an integer, the alphabet, and the input limits. Only the two named operations are modeled.

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 containing a and an index containing b. The two indices must be distinct, even when a == b. Return -1 when 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] contributes shortestDistance(a,b).
  • ["anagram",a,b] contributes 1 for true and 0 for 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 <= 1000 and 0 <= queries.length <= 1000.
  • Each entry of words and each query argument has between 1 and 40 lowercase ASCII letters.
  • Every query contains exactly three strings: its operation and two arguments.
  • The only operations are distance and anagram.

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
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int[] wordCollectionQueries(String[] words, String[][] queries) {
        // Write your code here.
    }

}
Checking account…