In this article, I am going to share 21 of the most common String-based Programming and Coding interview questions from Java developer interviews. These questions require you to write code to solve the problem and they are different from traditional Java String questions like how the substring method works in Java? or when to use the intern() method of String in Java? Since coding and problem solving are an important part of any programming job interview, it's imperative that you know how to solve them in time and in a pressure situation, which comes only after doing practice with the right set of questions. Since these questions are already tried and tested and appeared in many interviews, they will provide you the experience you need to crack your coding interview.
A string is one of the most popular data structures, probably next to the array and that's why you will find at least one question from String in any programming job interview. If you don't know, String is nothing but a character array in most of the languages like C. But, in Java, String is an object, which holds data in a character array.
This means you can solve many String-based problems by assuming it as a character array. You can use all the tips and tricks you learned by answering an array-based coding question to also solve String, like reversing a String is the same as reversing an array and so on.
I have also provided the links to solutions for most of the questions so that you can quickly check your answer and code. Remember, it's not just essential to solve the problem but also to understand the solution and improve it gradually.
In a real interview, an interviewer is as much interested in the solution as in improvement and optimization.
A string is one of the most popular data structures, probably next to the array and that's why you will find at least one question from String in any programming job interview. If you don't know, String is nothing but a character array in most of the languages like C. But, in Java, String is an object, which holds data in a character array.
This means you can solve many String-based problems by assuming it as a character array. You can use all the tips and tricks you learned by answering an array-based coding question to also solve String, like reversing a String is the same as reversing an array and so on.
I have also provided the links to solutions for most of the questions so that you can quickly check your answer and code. Remember, it's not just essential to solve the problem but also to understand the solution and improve it gradually.
In a real interview, an interviewer is as much interested in the solution as in improvement and optimization.
In order to improve the solution, good knowledge of data structure is mandatory because, in many cases, a choice of good data structure not only makes the solution simple but also fast. For example, by using a set, you can quickly figure out duplicate characters of String.
Also, you should be able to calculate the cost of your solution, like time and space complexity on Big O notation.
1) How to find the maximum occurring character in a given String? (solution)
Write an efficient Java/C/Python program to return the maximum occurring character in the input string, e.g., if the input string is "Java" then the function should return 'a'.
2) How to remove all duplicates from a given string? (solution)
Write a program to remove all the duplicate characters from a given input String, like, if the given String is "Java" then the output should be "Java". The second or further occurrence of duplicates should be removed.
3) How to print the duplicate characters from the given String? (solution)
Write an efficient Java/C/Python/Ruby program to return the duplicate characters from given String, for example, if given String is "C++" then your program should print "+" Similarly, if the input is "Java and JavaScript" then your program should print "J", "a" and "v". You can ignore the case for finding duplicates.
Also, you should be able to calculate the cost of your solution, like time and space complexity on Big O notation.
21 String Programming and Coding Interview Questions
Here are 21 most frequently asked String-based questions from a programming job interview. These questions are not based on theoretical concepts, like Why String is Immutable in Java, but slightly based upon the string data structure and require a lot of coding practice to solve them in limited time.1) How to find the maximum occurring character in a given String? (solution)
Write an efficient Java/C/Python program to return the maximum occurring character in the input string, e.g., if the input string is "Java" then the function should return 'a'.
2) How to remove all duplicates from a given string? (solution)
Write a program to remove all the duplicate characters from a given input String, like, if the given String is "Java" then the output should be "Java". The second or further occurrence of duplicates should be removed.
3) How to print the duplicate characters from the given String? (solution)
Write an efficient Java/C/Python/Ruby program to return the duplicate characters from given String, for example, if given String is "C++" then your program should print "+" Similarly, if the input is "Java and JavaScript" then your program should print "J", "a" and "v". You can ignore the case for finding duplicates.
If you notice there is a pattern to solve these duplicate characters problem. For example, you can use a data structure like HashSet or HashMap to store characters and their count to find out which is duplicate and how many times they are appearing. Knowing these coding tips and patterns really help during coding interviews.
4) How to remove characters from the first String which are present in the second String? (solution)
Write an efficient Java/c/Python function that takes two strings as arguments and removes the characters from the first string, which are present in the second string. For example, if the first String "India is great" and the second String is "in" then the output should be "da s great"
5) How to check if two strings are rotations of each other? (solution)
Write an efficient program to test if two given String is a rotation of each other or not, e.g. if the given String is "XYZ" and "ZXY" then your function should return true, but if the input is "XYZ" and "YXZ" then return false.
6) How to reverse a given String? (solution)
Write a program to reverse a String in C/Java/Python or choice of your programming language. You can write either the recursive or iterative solution. For example, if a given input is "abcd," then your function should return "dcba".
7) How to reverse String without recursion? (solution)
Same as the previous program, but you cannot use recursion, this time, you must solve this problem in C/Java/Python using iteration or loops. If you are not familiar with recursion and repetition.
8) How to print all permutation of a String? (solution)
Write an efficient program to print all permutations of a given String in Java/C/Python or any programming language of your choice. For example, if given input is "123" then your program should print all 6 permutations e.g. "123", "132", "213", "231", "312" and "321".
9) How to find the first non-repeating character in a given String? (solution)
Write a program in any programming language you to find the first non-repeated character in a given String, for example, if the given String is "Java" then the first non-repeated character is "J"
10) How to reverse the words in a given String sentence? (solution)
Write an efficient program in Java or C++ to reverse the words in a given String sentence. For example, if the input is "Java is best," then your program should print "best is Java". There is no restriction on preserving white space.
11) How to find the smallest substring in a given string containing all characters of another string? (solution)
Write an efficient function in C/C++ or Java to print the smallest substring in a given string containing all characters of another String. For example, if the given String is "this is a test string" and "tist", then the output should be "t stri".
12) How to check if two given String is the anagram of each other? (solution)
Write an efficient program in Java/C/C++ to check if two String is an anagram of each other. An anagram contains are of the same length and contains the same character, but in a different order, for example, "Army" and "Mary" is the anagram. Your program should return true if both Strings are the anagram, false otherwise
13) How do you check if a given String is Palindrome or not? (solution)
Write an efficient program in your favorite programming language to check if a Given a string is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"121" is a palindrome, but "123" is not. Your function should return true if given String is a palindrome, false otherwise
14) How do you convert String to an integer? (solution)
Implement atoi() like function in C/C++/Java to convert a string to an integer. Consider all possible cases e.g. positive and negative String, the presence of + or - character, etc. For example, if the given input String is "123" then your program should return 123 and if a given input is "+231" then your program should return 231.
I first learned about this problem on Programming Interviews Exposed: Secrets to Landing Your Next Job, one of the finest books on interviews, and still its explanation is the best I have read so far.
15) How do you convert a Roman numeral String to Integer in Java? (solution)
Write a function in your favorite programming language to convert a given Roman numeral to equivalent String. For example, if the given String is "X" then your program should print 10. Input will be within the range from 1 to 2000. You can also ignore case, like both "x" and "X" should return 10
17) How do you remove a given character from String? (solution)
Write an efficient method in Java to remove all occurrences of a given character in Java. For example, if the given String is "Programming" and the given character to remove is "m" then your function should return "Prograing".
18) How do you count the number of words in String? (solution)
Write a program to count a number of words in a given String. The words are separated by the following characters: space (‘ ‘) or newline (‘\n’) or tab (‘\t’) or a combination of these. For example, if input "Java is great" your program should print 3.
19) How do you check if a given string contains valid parentheses? (solution)
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', write a function in Java or C++ to check if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not
20) How do you find the longest palindromic substring of a given substring? (solution)
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.
This problem can be solved using the Dynamic Programming technique.
21) How to convert Integer to Roman String? (solution)
This is the reverse of problem 17, In this problem, you need to write a function in Java/C or C++ to convert a given integer into an equivalent Roman numeral. For example, if a given integer is 5, then your program should print "V".
Here is a chart of Roman numerals for 1 to 100 for your reference:
That's all about some frequently asked String Programming interview questions for preparing Programming Job interviews. The string is an important topic, so leave it at your peril. You might have seen this question before, but solving them again and understanding their performance will help you a lot in doing well in programming interviews.
Other Data Structure and Algorithms Resources you may like
- 75+ Coding Problems from Interviews (questions)
- 10 Free Courses to learn Data Structure and Algorithms (courses)
- 10 Books to Prepare Technical Programming/Coding Job Interviews (books)
- 10 Courses to Prepare for Programming Job Interviews (courses)
- 100+ Data Structure and Algorithms Interview Questions (questions)
- My favorite Free Algorithms and Data Structure Courses on FreeCodeCamp (courses)
- 30+ linked list interview questions with a solution (linked list)
- 30+ array-based interview questions for programmers (array)
- 10 Coding Tips and 101 Coding Questions for Interviews (tips)
- 50+ Algorithm based Interview questions from HackerNoon (questions)
- 5 Free Courses to learn Algorithms in-depth (courses)
- 10 Algorithms books every Programmer should read (books)
- Top 5 Data Structure and Algorithms Courses for Programmers (courses)
Thanks for reading this article so far. If you like this article, then please share it with your friends and colleagues. If you have any questions or feedback, then please drop a note.
Hi guys,
ReplyDeletehere the first question is "How to find the maximum occurring character in given String?"
but the solution link is for "How to Count Occurrences of a Character in String"
Both are different, Please confirm...
Yes, you are right but once you know how to count character then the other one becomes simple because you just need to return the character with maximum count
Deleteboolean b=true;
Deleteyes both are different
ReplyDeleteexample
1)How to find the maximum occurring character in given String?"
Malyalaum - a-3 (a is the maximum occurring character in given string)
2) How to Count Occurrences of a Character in String
Malyalaum
m-2
a-3
y-1
u-1
thank you,
That's correct but once you know how to count character then the other one becomes simple because you just need to return the character with maximum count
Deletepublic class OccObj {
Deletepublic static void main(String[] args){
Scanner input=new Scanner(System.in);
String s= input.next();
NoOfOcuu(s);
}
public static void NoOfOcuu(String s){
int c=0,c2=0;
ArrayListn=new ArrayList<>();
char c1='0';
for(int i=0;i=i)&&(c2==0)) {
if (s.charAt(i) == s.charAt(j)) {
c++;
}
}
}
if(c>1){
n.add(s.charAt(i));
}
if(c>0) {
System.out.println("the vale:" + s.charAt(i) + " - " + c);
}
}
}
}
public static void NoOfOcuu(String s){
ReplyDeleteint c=0,c2=0;
ArrayListn=new ArrayList<>();
char c1='0';
for(int i=0;i=i)&&(c2==0)) {
if (s.charAt(i) == s.charAt(j)) {
c++;
}
}
}
if(c>1){
n.add(s.charAt(i));
}
if(c>0) {
System.out.println("the vale:" + s.charAt(i) + " - " + c);
}
}
}
val str = "aabbbbaa"
ReplyDeletevar l = str.length
var s = ""
while (l>0){
s = s + str.get(l-1)
l--
}
if(s.equals(str)) {
println("palindrome string : " + s)
}else{
println("No palindrome string : " + s)
}
Hi guys,
ReplyDeletehere is the solution for question number 4 "How to remove characters from the first String which are present in the second String?"
public static void main(String[] args) {
String st1 = "india is great";
String st2 = "in";
List secondStringList = st2.chars().mapToObj(c -> (char) c).collect(Collectors.toList());
String outputString = st1.chars().mapToObj(c -> (char) c).filter(c -> !secondStringList.contains(c)).map(ch -> new String(String.valueOf(ch)))
.collect(Collectors.joining(""));
System.out.println("outputString : " + outputString);
}