Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Reverse Words and Check Anagrams

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

Your question is Reverse Words and Check Anagrams. 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

For a text-processing utility used in Alten Calsoft Labs tooling, implement one function that performs two independent string operations. First, reverse the order of words in a sentence. Second, determine whether two strings are anagrams.

A word is any maximal sequence of non-whitespace characters. Preserve each word's characters exactly, collapse consecutive whitespace between output words, and return an empty string for input containing only whitespace. For the anagram check, comparison is case-insensitive and ignores every character that is not an ASCII letter or digit.

Formal Specification

Implement analyze_strings(s1, s2). It receives two strings and returns a dictionary with:

  • reversed: the words of s1 in reverse order, joined by one space.
  • anagram: a Boolean indicating whether normalized s1 and s2 contain the same letters and digits with the same frequencies.

The word reversal and anagram check must be computed independently. Do not use sorting for the primary solution.

Constraints

  • 0 <= len(s1), len(s2) <= 10^5
  • Inputs contain ASCII characters
  • Anagram normalization retains only ASCII letters and digits
  • Anagram comparison is case-insensitive
  • Words are separated by arbitrary whitespace

Function Signature

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