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
Reconstruct String From Vocabulary
00:00
5 left

Reconstruct String From Vocabulary

MediumPython

Problem

NewsBreak needs to validate whether a normalized text string can be fully reconstructed from an approved vocabulary. A vocabulary word may be used any number of times, and words may share prefixes.

Given a lowercase string s and a list vocabulary, return True if s can be split into one or more contiguous vocabulary words. Return False otherwise. The empty string is considered reconstructible.

Build a solution that remains efficient when the vocabulary is large and many words share prefixes. Do not reorder characters, skip characters, or use a vocabulary word more times than allowed by the input string. The function only needs to return a boolean, not the actual segmentation.

Formal Specification

  • Input: s, a lowercase string, and vocabulary, a list of unique lowercase strings.
  • Output: A boolean indicating whether every character in s belongs to a valid vocabulary word in some segmentation.
  • Vocabulary words may be reused.

Constraints

  • 0 <= len(s) <= 100000
  • 1 <= len(vocabulary) <= 10000
  • 1 <= len(word) <= 20 for every vocabulary word
  • All characters are lowercase English letters
  • Vocabulary words are unique and may be reused

Function Signature

def can_reconstruct(s, vocabulary):
Interviewer

Your question is Reconstruct String From Vocabulary. 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.