Learn Java and Programming through articles, code examples, and tutorials for developers of all levels.
Difference between UNION vs UNION ALL in SQL? Example
Can you join two unrelated tables in SQL? Cross Join Example
Difference between Primary key vs Candidate Key in SQL Database? Example
Difference between Clustered and Non-Clustered Indexes in Database? SQL Question Answer
The difference between Clustered and Nonclustered index in a relational database is one of the most popular SQL interview questions almost as popular as the primary key vs unique key, the difference between truncate and delete, and correlated vs noncorrelated subqueries. Indexes are a very important concept, it makes your queries run fast and if you compare a SELECT query which uses an indexed column to one who doesn't you will see a big difference in performance.
Difference between Primary key vs Unique key in SQL Database? Answer
The primary key and unique key are two important concepts in a relational database and are used to uniquely identify a row in a table. Both primary key and unique keys can identify a row uniquely but there is some subtle difference between them which we will see in this article. In fact, primary key vs unique is a popular SQL interview question along with classics like truncate vs delete and How to manage transactions in a database, mostly asked to fresher and 2 to 3 years experience guys in any programming language. SQL is not just limited to any DBA or PLSQL developer but it's an important skill even for Java programmers and you can expect SQL interview questions even in many Java interviews.
Where Is Python Used In The Real World?
Hello guys, if you are wondering where is Python used in real world before you decide whether to learn Python programming language or not then its actually a good question. While everyone is talking about Python's popularity, it make sense to do research and find out which industries in Software development is using Python as their primary language. You will be surprised to know that like Java Python is also almost everywhere. Python is now used in Artificial Intelligence, Web Development, Game Development, Building Desktop and UI Apps, Machine Learning, Data Science, Data Analysis, Building Mobile apps, Web Scrapping, and most importantly on Automation. So, you can see that Python is widely accepted and used and learning Python is the best decision you can take to start your career as Software Developer.
Top 10 tools for SQL Developers and Database Administrators
Hello guys, if you are a SQL database administrator a SQL Developer, a good knowledge of database tools can really help. It even been said that a craftsman is as good as his tools. So, you should spend some time and find out the tools which can help you with your job with respect to Database and SQL. Here are the main 10 free data set instruments that play out a great many explicit capabilities. The thought behind this rundown is to assist you with picking the right devices that best met your requirements.
Top 5 advanced JavaScript function concepts to Learn
A function is a piece or block of code that is created to perform single or multiple tasks. Every major programming language has functions because functions are one of the essential parts of modern programming. JavaScript also supports functions. Over time, JavaScript has upgraded functions in several ways to make them more programmer-friendly. In recent years, more and more advanced concepts are added in JavaScript. In this article, we are going to discuss the advanced working with functions in JavaScript.
Top 7 Gift Ideas for Programmers, Coders, and Software Developers in 2025 [UPDATED]
Top 20 UI and UX Design Interview Questions and Answers
Top 21 Chef Interview Questions and Answers for DevOps Engineers
Top 20 Shell Scripting Interview Questions and Answers
Have you struggle with Shell Scripting interview questions before? Have you been having problems in finding out the kind of questions asked in a Shell Scripting interview? If you have experienced this then it is high time that you find a solution. You cannot lose that dream job anymore and yet you can find with ease all you require to pass your interview. If that interview is a few days away, worry no more because you will get before that panel fully prepared and ready for the day by going through these common shell scripting questions.
Top 20 Bootstrap Interview Questions Answers for Web Designers and Developers
Hello guys, if you are preparing for web design and development interview and looking for Bootstrap interview questions then you have come to the right place. Bootstrap is one of the most popular CSS Framework and almost all websites uses Bootstrap for their classy look and feel and bigger buttons and form elements. Earlier, I have shared HTML 5 Interview Questions and best online courses to learn HTML, CSS and Bootstrap courses and today, I am going to share bootstrap interview questions with answers. Here is a chance for you to make sure that you go into that Bootstrap interview with your head up high. Being ready for that interview is the major step that you can make as you seek to impress the interview panel on the interview day.
Top 20 Artificial Intelligence Interview Questions and Answers
Top 20 Golang Interview Questions with Answers
Top 20 Hadoop Interview Questions and Answers for Java Developers
Top 20 Google Cloud Interview Questions with Answers
Top 20 Kubernetes Interview Questions Answers for 1 to 3 Years Experienced
Top 27 Dynamic Programming Interview Questions for Interviews
Top 20 Unity Interview Questions and Answers for Game Developers
Top 20 Unreal Engine Interview Question and Answers for Game Developers
Top 20 TypeScript Interview Questions and Answers for Web Developers
Top 5 Udemy Courses to Learn Distributed Systems in 2025
Hello guys, Distributed systems are the backbone of modern applications, enabling scalability, fault tolerance, and high availability. Whether you’re building microservices, cloud applications, or real-time systems, understanding distributed systems is crucial. If you want to learn Distributed System in 2025 and looking for resources then you have come to the right place. Earlier, I have shared best System Design Books, System Design Questions and best Places to learn System Design and in this article, I am going to share best Distributed System courses from Udemy, one of my favorite place to learn programming and technical stuff.
Is System Design Interview Inside Guide Vol 1 and 2 Book Worth Reading in 2025? Review
Hello guys, if you are preparing for System design interviews or Software Design Interviews, then you must have come across System Design Interview - An Insider's Guide by Alex Xu, one of the most popular book on System Design after Designing Data-Intensive Applications by Martin Kleppmann. I first come across Alex Xu on Twitter when one of his image about how HTTPS works went viral. The image was quite detailed and presentable so I start following Alex and then I come across ByteByteGo, his online System design course and his book System Design Interview - An Insider's Guide.
10 Best System Design Interview Courses for Software Engineers in 2025
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