Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Coding: Anagrams Using HashMap

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

Your question is Coding: Anagrams Using HashMap. 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

TBO tek receives booking-related text from different interfaces, where capitalization, spaces, and punctuation may vary. Write a function that determines whether two strings are anagrams after normalizing them.

Two strings are anagrams if they contain the same alphanumeric characters with the same frequencies, regardless of order. Treat uppercase and lowercase letters as equal, and ignore all non-alphanumeric characters.

Formal Specification

Implement are_anagrams(first, second).

  • Input: Two strings, first and second.
  • Output: Return True if the normalized strings are anagrams; otherwise, return False.
  • Normalization converts letters to lowercase and removes characters for which isalnum() is false.
  • The function must not modify either input string.

Constraints

  • 0 <= len(first), len(second) <= 10^5
  • Inputs may contain Unicode alphanumeric characters
  • Matching is case-insensitive
  • Non-alphanumeric characters are ignored
  • The expected target complexity is linear in the combined input length

Function Signature

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