Important Questions on Strings and Arrays
1) How to check if two Strings are anagrams of each other?
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.
2) How to program to print first non repeated character from String?
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.
3) How to count occurrence of a given character in String
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.
4) How to find all permutations of String?
I have seen this String interview question on many interviews. It has a easy recursive
solution but thinks get really tricky when Interviewer ask you to solve this question
without using recursion. You can use Stack though. Write a program to print all
permutations of a String in Java, for example, the if input is "xyz" then it should
print "xyz", "yzx", "zxy", "xzy", "yxz", "zyx".
5) How to check if a String is valid shuffle of two String?
One more difficult String algorithm based coding question for senior developers.
You are given 3 strings: first, second, and third. third String is said to be a
shuffle of first and second if it can be formed by interleaving the characters of
first and second String in a way that maintains the left to right ordering of the
characters from each string. For example, given first = "abc" and second
= "def", third = "dabecf" is a valid shuffle since it preserves the character
ordering of the two strings. So, given these 3 strings write a function that detects
whether third String is a valid shuffle of first and second String.
6) How to sort String on their length in Java?
Write a Program to sort String on their length in Java? Your method should accept
an array of String and return a sorted array based upon the length of String. Don't
forget to write unit tests for your solution.
7) Write a program to find longest palindrome in a string?
This is one of the tough coding question based upon String. It's hard to think about
an algorithm to solve this problem until you have practiced good. What makes it
more difficult is the constraint that your solution has O(n) time complexity and
O(1) space complexity.
8) Write a program to remove a given characters from String
One of my favorite coding question, when I interview Java developers. You need to
write a Java method which will accept a String and a character to be removed and
return a String, which doesn't has that character e.g remove(String word, char
ch). You need to provide both iterative and recursive solution of this method and
also has to cover cases like null and empty String, input which only contains letter to
be removed, String which doesn't contain given character etc.
ARRAY
1. How to find largest and smallest number in unsorted array?
This is a rather simple array interview question. You have given an unsorted integer
array and you need to find the largest and smallest element in the array. Of course,
you can sort the array and then pick the top and bottom element but that would cost
you O(NLogN) because of sorting, getting element in array with index is O(1)
operation.
2. How to find all pairs on integer array whose sum is equal to given number?
This is an intermediate level of array coding question, it's neither too easy nor too
difficult. You have given an integer array and a number, you need to write a program
to find all elements in the array whose sum is equal to the given number. Remember,
the array may contain both positive and negative numbers, so your solution should
consider that. Don't forget to write unit test though, even if the interviewer is not
asked for it, that would separate you from a lot of developers. Unit testing is always
expected from a professional developer.
3. Write a program to remove duplicates from array in Java?
This is another follow-up question from problem 2 and 6. You have given an array
which contains duplicates, could be one or more. You need to write a program to
remove all duplicates from array in Java. For example if given array is {1, 2, 1, 2, 3, 4,
5} then your program should return an array which contains just {1, 2, 3, 4, 5}. This
array question is also comes at intermediate category because there is no way to
delete an element from an array. If substituting with another value is not an option
then you need to create another array to mimic deletion.
4. Write a program to find intersection of two sorted arrays in Java?
Another interesting array interview question, where you need to treat the array
as Set. Your task is to write a function in your favorite language e.g. Java,
Python, C or C++ to return the intersection of two sorted arrays. For example,
if the two sorted arrays as input are {21, 34, 41, 22, 35} and {61, 34, 45, 21,
11}, it should return an intersection array with numbers {34, 21}, For the sake
of this problem you can assume that numbers in each integer array are unique.
5. There is an array with every element repeated twice except one. Find that
element?
This is an interesting array coding problem, just opposite of question related to finding
duplicates in array. Here you need to find the unique number which is not repeated
twice. For example if given array is {1, 1, 2, 2, 3, 4, 4, 5, 5} then your program should
return 3. Also, don't forget to write couple of unit test for your solution.
6. How to find kth largest element in unsorted array?
This problem is exactly same as previous question with only difference being finding
kth largest element instead of kth smallest number. For example if given array is {10,
20, 30, 50, 40} and k = 3 then your program should return 30 because 30 is the 3rd
largest number in array. You can also solve this problem by sorting the array in
decreasing order and picking k-1th element. I often seen this array question on Java
interviews with 2 to 3 years experienced guys.
Comments
Post a Comment