Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Repeated Words and Recursion

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

Your question is Repeated Words and Recursion. 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

Nykaa search and review text may contain inconsistent capitalization and punctuation. Given a sentence, return the words that occur more than once, along with the sentence reversed character by character using recursion.

Treat words case-insensitively. A word is a consecutive sequence of letters or digits, and punctuation does not belong to a word. Preserve the original sentence exactly when producing the reversed string, including spaces and punctuation.

Formal Specification

Implement analyze_sentence(sentence), where sentence is a string. Return a dictionary with:

  • repeated_words: a dictionary mapping each repeated lowercase word to its occurrence count. Include only words with a count greater than one.
  • reversed: the original sentence reversed character by character, produced by a recursive helper.

The order of entries in repeated_words does not affect correctness.

Constraints

  • 0 <= len(sentence) <= 10^5
  • Words consist of ASCII letters and digits.
  • Punctuation and whitespace may appear anywhere.
  • Word matching is case-insensitive.
  • The recursive reversal must not use slicing, reversed(), or [::-1].

Function Signature

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