Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Longest Dictionary Word
00:00
5 left

Longest Dictionary Word

HardPython

Problem

Avathon's AI platform receives compacted text labels from an edge pipeline. Given a source string and a dictionary, return the longest dictionary word that can be formed by deleting zero or more characters from the source while preserving character order.

If multiple words have the maximum length, return the lexicographically smallest one. If no dictionary word can be formed, return an empty string.

Formal Specification

Implement longest_dictionary_word(source, dictionary).

  • source is a lowercase English string.
  • dictionary is a list of lowercase English words.
  • Return one dictionary word as a string.
  • A word is formable when it is a subsequence of source. Characters do not need to be contiguous, and each source character can be used at most once.

To support large inputs, preprocess the source so each next-character lookup is efficient. Avoid comparing every candidate by repeatedly scanning the source from its beginning.

Constraints

  • 1 <= len(source) <= 100000
  • 1 <= len(dictionary) <= 100000
  • The total number of characters across dictionary words is at most 1000000
  • All characters are lowercase English letters
  • Dictionary entries may be duplicated

Function Signature

def longest_dictionary_word(source, dictionary):
Interviewer

Your question is Longest Dictionary Word. Start with the requirements in the Question tab.

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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.