Challenge - 5 Problems
Word Break Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Word Break Check
What is the output of the following Java code that checks if the string can be segmented into dictionary words?
DSA Java
import java.util.*; public class WordBreak { public static boolean wordBreak(String s, Set<String> dict) { boolean[] dp = new boolean[s.length() + 1]; dp[0] = true; for (int i = 1; i <= s.length(); i++) { for (int j = 0; j < i; j++) { if (dp[j] && dict.contains(s.substring(j, i))) { dp[i] = true; break; } } } return dp[s.length()]; } public static void main(String[] args) { Set<String> dict = new HashSet<>(Arrays.asList("apple", "pen")); System.out.println(wordBreak("applepenapple", dict)); } }
Attempts:
2 left
💡 Hint
Think about whether the string can be split into dictionary words.
✗ Incorrect
The string "applepenapple" can be segmented as "apple" + "pen" + "apple", all present in the dictionary, so the output is true.
❓ Predict Output
intermediateOutput for Unbreakable String
What will be the output of this Java code when the string cannot be segmented into dictionary words?
DSA Java
import java.util.*; public class WordBreak { public static boolean wordBreak(String s, Set<String> dict) { boolean[] dp = new boolean[s.length() + 1]; dp[0] = true; for (int i = 1; i <= s.length(); i++) { for (int j = 0; j < i; j++) { if (dp[j] && dict.contains(s.substring(j, i))) { dp[i] = true; break; } } } return dp[s.length()]; } public static void main(String[] args) { Set<String> dict = new HashSet<>(Arrays.asList("cats", "dog", "sand", "and", "cat")); System.out.println(wordBreak("catsandog", dict)); } }
Attempts:
2 left
💡 Hint
Check if the string can be fully segmented using the dictionary words.
✗ Incorrect
The string "catsandog" cannot be segmented fully into the dictionary words because "og" is not in the dictionary, so output is false.
🧠 Conceptual
advancedUnderstanding DP Array in Word Break
In the Word Break problem, what does the boolean array dp represent during the algorithm execution?
Attempts:
2 left
💡 Hint
Think about what the index i in dp corresponds to in the string.
✗ Incorrect
dp[i] is true if the substring from start to i-1 can be segmented into dictionary words, helping build solutions for longer substrings.
🔧 Debug
advancedIdentify the Bug in Word Break Code
What error will this Java code produce when run, and why?
DSA Java
import java.util.*; public class WordBreak { public static boolean wordBreak(String s, Set<String> dict) { boolean[] dp = new boolean[s.length()]; dp[0] = true; for (int i = 1; i <= s.length(); i++) { for (int j = 0; j < i; j++) { if (dp[j] && dict.contains(s.substring(j, i))) { dp[i] = true; break; } } } return dp[s.length()]; } public static void main(String[] args) { Set<String> dict = new HashSet<>(Arrays.asList("apple", "pen")); System.out.println(wordBreak("applepenapple", dict)); } }
Attempts:
2 left
💡 Hint
Check the size of dp array and the indices used to access it.
✗ Incorrect
The dp array size is s.length(), but code accesses dp[s.length()] which is out of bounds causing ArrayIndexOutOfBoundsException.
🚀 Application
expertCount Number of Ways to Segment String
Given the Word Break problem, which code snippet correctly counts the number of ways to segment the string into dictionary words?
Attempts:
2 left
💡 Hint
Count ways requires summing counts from valid previous breaks, not just boolean flags.
✗ Incorrect
Option D uses an integer dp array to accumulate counts of ways to segment prefixes, correctly summing counts from valid previous indices.
