Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Anagram Finder With Tests

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Anagram Finder With Tests. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Milliman Integrate may need to resolve multiple search terms against a terminology dictionary, treating words as matches when they contain exactly the same letters in a different order. Given a dictionary and several query words, return every dictionary word that is an anagram of each query.

The result for each query must preserve the dictionary's original order, including duplicate dictionary entries. A dictionary word matches only when its character counts are identical to the query's counts. Do not return generated permutations that are absent from the dictionary.

Formal Specification

Implement find_anagrams(dictionary, queries), where dictionary and queries are lists of lowercase English strings. Return a list whose ith element contains all dictionary words that are anagrams of queries[i].

Build reusable lookup information so that the dictionary is processed once rather than rescanned for every query.

Constraints

  • 1 <= len(dictionary), len(queries) <= 10^5
  • Each word contains 1 to 20 lowercase English letters
  • The total number of dictionary characters is at most 2 * 10^6
  • The total number of query characters is at most 2 * 10^6
  • Dictionary duplicates must be preserved

Function Signature

def find_anagrams(dictionary, queries):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output