Asked inAmazon,SDE-2

Word Break Problem Statement

You are given a non-empty string without spaces, referred to as 'sentence', and a list of non-empty strings representing a dictionary. Your task is to construct and return all possible sentences by inserting spaces in the given 'sentence' such that each segmented word is found in the dictionary.

Note:

The same word from the dictionary can be used multiple times to form sentences.

Input Format:

The first line contains an integer ‘N’ indicating the size of the dictionary.
Each of the next ‘N’ lines contains a non-empty string representing each word in the dictionary.
Following that, there is a single non-empty string representing the 'sentence'.

Output Format:

Output each possible sentence after adding spaces, each on a new line.
Note:

There is no need to print anything manually; output handling is already managed. The order of sentences does not matter.

Constraints:

  • 1 ≤ N ≤ 100
  • 1 ≤ M ≤ 16
  • 1 ≤ W ≤ 16

Where ‘N’ is the number of dictionary words, ‘W’ is the length of a word in the dictionary, and ‘M’ is the length of the sentence. Time limit: 1 second.

AnswerBot
4mo

Given a sentence and a dictionary, construct and return all possible sentences by inserting spaces to form words from the dictionary.

  • Use backtracking to generate all possible combinations of words fro...read more

Help your peers!
Select
Add answer anonymously...

Amazon SDE-2 interview questions & answers

A SDE-2 was asked Q. Given a binary tree where each node contains a digit from 0-9, find the sum of a...read more
A SDE-2 was asked Q. Write a program to perform a preorder traversal of a binary tree without using r...read more
A SDE-2 was asked Q. Write code to find the median of an unsorted array.

Popular interview questions of SDE-2

A SDE-2 was asked Q1. Given a binary tree where each node contains a digit from 0-9, find the sum of a...read more
A SDE-2 was asked Q2. Write a program to perform a preorder traversal of a binary tree without using r...read more
A SDE-2 was asked Q3. Write code to find the median of an unsorted array.
Amazon SDE-2 Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits