1) How to Print duplicate characters from String? (solution)
To start with, we have a simple String related coding question frequently asked in programming interviews. You need to write a program in C, C++, Java or Python to print duplicate characters from a given String, for example if String is “Java” then program should print “a”. Bonus points if your program is robust and handle different kinds of input e.g. String without duplicate, null or empty String etc. Bonus points if you also write unit tests for normal and edge cases.
2) How to check if two Strings are anagrams of each other? (solution)
A simple coding problem based upon String, but could also be asked with numbers. You need to write a Java program to check if two given strings are anagrams of Each other. Two strings are anagrams if they are written using the same exact letters, ignoring space, punctuation and capitalization. Each letter should have the same count in both strings. For example, Army and Mary are anagram of each other.
3) How to program to print first non repeated character from String? (solution)
One of the most common string interview questions: Find the first non-repeated (unique) character in a given string. for Example if given String is “Morning” then it should print “M”. This question demonstrates efficient use of Hashtable. We scan the string from left to right counting the number occurrences of each character in a Hashtable. Then we perform a second pass and check the counts of every character. Whenever we hit a count of 1 we return that character, that’s the first unique letter. Be prepared for follow-up question for improving memory efficiency, solving it without hash table as well.
4) How to reverse String in Java using Iteration and Recursion? (solution)
Your task is to write a program to reverse String in Java without using StringBuffer class. You also need to provide both iterative and recursive algorithm for String reversal. You can use other String utility methods e.g. charAt(), toCharArray() or substring() from java.lang.String class.
5) How to check if a String contains only digits? (solution)
You need to write a program to check a String contains only numbers by using Regular expression in Java. You can use Java API but a solution without using Java API will be better because that is what interviewer can always ask.
6) How to find duplicate characters in a String? (solution)
You need to write a program to print all duplicate character and their count in Java. For example if given String is “Programming” then your program should print
g : 2
r : 2
m : 2
7) How to count number of vowels and consonants in a String? (solution)
One of easiest String question you will ever see. You have to write a Java program which will take a String input and print out number of vowels and consonants on that String. For example if input is “Java” then your program should print “2 vowels and 2 consonants”. If you get this question on Interview, you should clarify that whether String can contain numbers, special characters or not e.g. anything other than vowels and consonants.
8) How to count occurrence of a given character in String? (solution)
If interviewer ask you to count occurrence of more than one character than you can either use an array, hash table or any additional data structure. In order to solve this problem, you are not allowed to do so. Your method must return count of given character, for example if input String is “Java” and given character is ‘a’ then it should return 2. Bonus point if you handle case, null and empty String and come up with unit tests.
9) How to convert numeric String to int? (solution)
A classical coding interview question based upon String. You need to write a method like atoi() from C/C++, which takes a numeric String and return its int equivalent. For example, if you pass “67263” to the program then it should return 67263. Make sure your solution is robust i.e. it should be able to handle + and – character, null and empty String, integer overflow and other corner cases. Bonus points if you come up with good unit test cases. By the way, if your interviewer doesn’t mention to you about atoi() then you can also use Java API’s parseInt() or valueOf() method to solve this problem.