Learn Java and Programming through articles, code examples, and tutorials for developers of all levels.
Top 15 Courses to Learn System Design and Software Architecture in 2025 - Best of Lot
Review - Is AlgoMonster Good Resource For Coding Interview Preparation in 2025?
Hello guys, if you are preparing for coding interviews and wondering whether joining AlgoMonster is right decision or not then you have come to the right place. Earlier I have shared best website for coding interview preparation, books, and best coding interview courses and in this article, we will review Algo Monster, one of the top site for coding interview preparation. Before we review Algomonster, let me tell you what it really is. For those of you who don't know, Algomonster is a legit site and basically an interview prep site with a focus on coding that will help you clear your interview and land your dream coding job.
Top 8 Online Courses to Learn System Design and Software Architecture on Udemy (2025)
Hello friends, If you're aiming to master Software Architecture and System Design in 2025, you're in the right place. Whether you're preparing for tough technical interviews or simply want to become a better software engineer, understanding system design is a must. But let’s be honest—it’s also one of the most challenging topics to master. Many developers, including experienced ones, struggle with system design interviews—especially when applying to top tech companies like Google, Meta, Amazon, Apple, Microsoft, and Netflix (formerly known as FAANG, now MAANG).
70+ Coding Interview Questions for Software Engineers and Developers in 2025
Difference between Daemon Thread vs User Thread in Java? Example
How to join two threads in Java? Thread.join() example
How to stop a thread in Java? Example
10 Things about Threads Every Java Programmer Should Know
Difference between int and Integer Types in Java? Example
What is Thread and Runnable in Java? Example
What is blank final variable in Java - Example
Access Modifiers in Java - Public, Private, Protected, and Package Examples
How to display date in multiple timezone in Java with Example - PST GMT
JDBC - How to Convert java.sql.Date to java.util.Date in Java with Example
5 Essential JDK 7 Features for Java Programmers
It's close to a year now JDK 7 or Java 7 has been released, still, programmer asks what is new in Java 7 ? What is the best feature introduced in JDK7, where is the list of all Java 7 new features, etc? I thought to let's document Top 5 new features introduced in Java 7 for easy reference, this will not only answer What is new in Java 7 but also provide a quick overview of What are those features in Java 7. Java 7 introduced many new features in Java programming language like try-catch with a resource, String in Switch, etc but it also makes a lot of changes on Java Development API by introducing a new File API in Java and several other minor changes. Like you can now find hidden files from the Java programs without applying any hack. anyway let's see my list of Top 5 Java 7 features :
Difference between DOM vs SAX Parser in Java - XML Parsing in Java
DOM and SAX parser are the two most popular parsers used in the Java programming language to parse XML documents. DOM and SAX concept is originally XML concept and Java programming language just provide an API to implement these parsers. Despite both DOM and SAX are used in XML parsing, they are completely different from each other. In fact difference between DOM and SAX parser is a popular Java interview question asked during Java and XML interviews. DOM and SAX parser has a different way of working, which makes Java programmer understand the difference between DOM and SAX parser even more important.
How to configure Daily Log File Rolling in Java using Log4j - DailyRollingFileAppender Example
Eclipse and NetBeans Keyboard Shortcuts for Java Programmers - Example
How to convert int value to a Long object in Java? Examples
How to Create Random Alphabetic or AlphaNumeric String of given length in Java? SecureRandom Example
How to Order and Sort Objects in Java? Comparator and Comparable Example
Difference between int and Integer data type in Java? Example
JDBC - How to get Row and Column Count From ResultSet in Java? Example
Can You Create Instance of Abstract class in Java? Answer
How to convert String to Enum in Java? ValueOf Example
The Ultimate Guide to Package in Java? Examples
How to read a file line by line in Java? BufferedReader Example
Java Enum with Constructor Example
Could not create the Java virtual machine Invalid maximum heap size: -Xmx
ArrayList vs Vector in Java? Interview Question Answer
ArrayList and Vector are the two most widely used Collection classes in Java and are used to store objects in an ordered fashion. Every Java programmer which is introduced to Java Collection Framework either started with Vector or ArrayList. For beginners Difference between Vector and ArrayList in Java and LinkedList vs ArrayList are the two most popular Java Interview questions. ArrayList vs Vector is not only important from an interview perspective but also on the effective use of Java Collection API.
The Ultimate Guide of Enum in Java - Examples
What is class file in Java? Example
Class file in Java is compiled from of Java source file. When we compile a Java program written in a Java source file ended with a .java extension, it produces one more class file depending upon how many classes are declared and defined in that Java source file. One Java source file can only contain one public class, and its name must match with the name of the file like HelloWorld.java file can contain a public class whose name should be HelloWorld as shown below :
Difference between static and non static nested class in Java? Example
How to read file in Java using Scanner Example - text files
Difference between throw vs throws in Java? Answer
How to read User Input from Console in Java? Scanner Example
How to Find IP address of localhost or a Server in Java? Example
15 People Java Developers Should Follow on Twitter
Java Keyword Cheat Sheet - Meaning and Usage
Video example - Dijkstra's Algorithm shortest path in Graph
Video example - Dijkstra's Algorithm shortest path in Graph
How to remove duplicate(s) from linked list in Java? Example Tutorial
How to find Factorial in Java using Recursion and Iteration - Example Tutorial
How to calculate perimeter and area of square in Java? Example Tutorial
How to solve word break problem in Java using dynamic programming? Example
You are given a dictionary of words and the input string. Determine input string can be segmented into a space-separated sequence of given dictionary words.
Note: This question is based on dynamic programming and asked multiple times in top product-based companies.
Inputs:
Dict = {i, like, am, boy, e, o, dog, cat, g};
word = "iIikedog" --------can be segmented into space-separated words--------> i, like, dog
Ask yourself that 'I', 'like', and 'dog' are presented in the dictionary?
Yes, so we can say that the given string can be segmented.
Now let's take another example.
word = "ilikecatsanddog" --------- can be segmented as-----> i, like, cats, and, dog
i, like, cat, s, and, dog
We can see that the words 'cats', 'and', 's' are not present in the dictionary so such a string can't be segmented into space-separated words.
Let's try to solve it.....
Like another dynamic programming problem, we will create a matrix and will use previously calculated results to calculate the current result.Consider the given string as an array like this.
i | l | i | k | e | d | o | g |
The columns and rows represent the same given string in the matrix.
Now let's try to understand what each cell represents in the matrix.
The cells can have two kinds of values either '0' or '1'. Suppose the cells (3,6) and (3,7) have the value '1' this means the substrings from 3 to 6 and 3 to 7 are present in the given input dictionary.
If you notice the cell (5,3) and the corresponding substring in the given word the direction is reverse and there is no point in considering the reverse computation of the given string. So we will mark all such cells as '0'.
We filled half of the matrix with value '0' so we no need to perform the reverse computations for these cells. This way we can save both memory and time.
Now start filling rest of the cells manually by comparing the row-column pair value against the given dictionary.
So the cell (0, 0) i.e. 'i' is present in the dictionary so matrix[0][0] = 1.
Similarly cell (4, 4) i.e. 'e' is also present in the dictionary so matrix[4][4] = 1.
Now let's take one substring 'ilike' and see the logic to solve this problem.
i | l | i | k | e | d | o | g |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
i l i k e
0 1 2 3 4
We will try to separate this string into 2 parts in all the possible ways.
Let say we have first 2 parts:
(0, 0)--------> 'i' and (1, 1)----------> 'l'
matrix[0][0] && matrix[1][1]
1 && 0
So 'il' is not present in the dictionary.
Consider another combination.
(4, 3)--------> 'k' and (4, 4)----------> 'e'
matrix[4][3] && matrix[4][4]
0 && 1
This is false means 'ke' is not present in the dictionary.
So I will keep on taking the substrings from the given word to be space separated and divide them into 2 parts in different ways and will check against the matrix that if both the parts are true i.e. '1' means the substring is present in the dictionary.
So finally we will consider the entire given word and divide it into 2 parts in all the possible ways and we will use the values of the previously computed cells to calculate the value of the marked cell and if the the value is '1' means the given word can be separated into the space separated segments.
Start Implementing it...
Consider below code snippet.
i | l | i | k | e | d | o | g |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
For i = 0 and N = 7
for(int k = 0; k < N+1; k++){
if(matrix[i][i+k]) && matrix[k+1][N])
{
return true;
}
}
The if condition shows the logic to divide the given word into 2 parts in all possible ways.
Complete Code:
public class Main {
public String wordBreakProblem(String word, Set<String> dict){
int matrix[][] = new int[word.length()][word.length()];
// fill all the cells with '-1'.
for(int i=0; i < matrix.length; i++){
for(int j=0; j < matrix[i].length ; j++){
matrix[i][j] = -1;
}
}
//If the substring is present in the dictionary then fill the corresponding cell with non-negative value.
for(int l = 1; l <= word.length(); l++){
for(int i=0; i < word.length() -l + 1 ; i++){
int j = i + l-1;
String str = word.substring(i,j+1);
if(dict.contains(str)){
matrix[i][j] = i;
continue;
}
// Filling the value of the corresponding cell for the taken substring using value of the previously calculated cell.
for(int k=i+1; k <= j; k++){
if(matrix[i][k-1] != -1 && matrix[k][j] != -1){
matrix[i][j] = k;
break;
}
}
}
}
if(matrix[0][word.length()-1] == -1){
return null;
}
//Finally segregate the given word into the words available in the dictionary.
StringBuffer buffer = new StringBuffer();
int i = 0; int j = word.length() -1;
while(i < j){
int k = matrix[i][j];
if(i == k){
buffer.append(word.substring(i, j+1));
break;
}
buffer.append(word.substring(i,k) + " ");
i = k;
}
return buffer.toString();
}
public static void main(String args[]){
Set<String> dictionary = new HashSet<String>();
dictionary.add("I");
dictionary.add("like");
dictionary.add("had");
dictionary.add("play");
dictionary.add("to");
String str = "Ihadliketoplay";
Main bmw = new Main();
String result1 = bmw.wordBreakProblem(str, dictionary);
System.out.print(result1);
}
}
Output:
I had like to play
Test your understanding...
Q. 1) Given dict = {'li, 'sop', 'tree', 'ding', 'g'} and word = 'sopptreeg'. Is it possible to segment the given string into space separated segments of the given dict. words ?
Ans. 'sop' , 'p', 'tree', 'g'
'so', 'pp', 'tree', 'g'
It looks like the given word can't be separated.
Before you leave...
Knowledge of data structure and algorithms is must to simulate the real world problem in code.If you want to learn more about this article, drop a comment below and reach out to us to let us know your interest.
If you enjoyed learning the fundamentals of DSA share your knowledge to your fellow programmers and social circle. May be someone out really needs this resource, and you might be helping them out by sharing it.
eeeeellldldkonoioid