Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Duplicate Words From a String

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

Your question is Duplicate Words From a String. 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

An eClinicalWorks clinical note may contain repeated words with different capitalization or punctuation. Write a function that returns every duplicated word exactly once, preserving the order in which each word first appears as a duplicate.

A word is a contiguous sequence of letters or digits, optionally containing internal apostrophes. Matching is case-insensitive, and returned words must be lowercase. Ignore punctuation and whitespace. A word is considered duplicated when it appears at least twice in the input.

Formal Specification

  • Input: text, a string containing zero or more words, spaces, and punctuation.
  • Output: A list of lowercase strings. Each returned word appears at least twice, with no duplicates in the result.
  • Ordering: Results follow the first-occurrence order of the duplicated words in the original text.

Constraints

  • 0 <= len(text) <= 10^5
  • Words consist of letters or digits, with optional internal apostrophes
  • Matching is case-insensitive
  • Return each duplicated word exactly once
  • Preserve the first-appearance order of duplicated words

Function Signature

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