String Programming Interview Question
String Programming Interview Question
The string is a primary and probably most common thing you come across on any
programming language and so is with any programming interview. There is almost
always a question on String whether its related to length or replace but I have always
found one or two String programming questions on interviews.
2) Write a method which will remove any given character from a String? (solution)
hint: you can remove a given character from String by converting it into a character
array and then using substring() method for removing them from output string.
3) Print all permutation of String both iterative and Recursive way? (solution)
5) How to find the first non repeated character of a given String? (solution)
Some more String related Questions which mostly appear in Java programming
interviews:
4) Why Char array is preferred over String for storing password? (answer)
9) In an array 1-100 numbers are stored, one number is missing how do you find
it? (solution)
10) In an array 1-100 exactly one number is duplicate how do you find it?
(solution)
11) In an array 1-100 multiple numbers are duplicates, how do you find
it? (solution)
One trick in this programming questions is by using HashMap or Hashtable, we can
store a number as key and its occurrence as value if the number is already present in
Hashtable then increment its value or insert value as 1 and later on print all those
numbers whose values are more than one.
12) Given two arrays, 1,2,3,4,5 and 2,3,1,0,5 find which number is not present in
the second array.
Here is a quick tip to solve this programming question: put the elements of the second
array in the Hashtable and for every element of the first array, check whether it’s
present in the hash or not, O/P all those elements from the first array that are not
present in the hash table
13) How do you find the second highest number in an integer array? (solution)
14) How to find all pairs in an array of integers whose sum is equal to the given
number? (solution)
15) How to remove duplicate elements from the array in Java? (solution)
16) How to find the largest and smallest number in an array? (solution)
17) How to find the top two maximum number in an array? (solution)
For example, the array needs contiguous memory to store objects but the linked list
doesn't need that. It's difficult to add and remove elements in an array because you
need to shift existing elements but that is very easy with a linked list, as you just
need to change the pointer to accommodate them.
But, nothing is free in this world. While linked list provides all these functionalities
but the cost of that you lose the ability to search elements in constant time with
index. Searching and element require traversing linked list, which means examining
all nodes, thus cost around O(n) time.
14) How do you find middle element of a linked list in a single pass?
To answer this programming question I would say you start with a simple solution on
which you traverse the LinkedList until you find the tail of linked list where it points
to null to find the length of the linked list and then reiterating till middle.
After this answer interviewer will ask you to find the middle element in single pass
and there you can explain that by doing space-time trade-off you can use two pointers
one incrementing one step at a time and other incrementing two-step a time, so when
the first pointer reaches end of linked second pointer will point to the middle
element.
15) How do you find the 3rd element from last in a single pass? (solution)
This programming question is similar to above and can be solved by using 2 pointers,
start the second pointer when the first pointer reaches third place.
16) How do you find if there is any loop in a singly linked list? How do you find the
start of the loop? (solution)
This programming question can also be solved using 2 pointers and if you increase one
pointer one step at a time and other as two steps at a time they will meet in some
point if there is a loop.
18) Difference between a linked list and array data structure? (answer)
21) Write a method in Java to check if a tree is a binary search tree or not? (solution)
24) How do you perform preorder traversal in a given binary tree? (solution)
25) How do you traverse a given binary tree in preorder without recursion? (solution)
26) How do you print all nodes of a given binary tree using inorder traversal without
recursion? (solution)
27) How do you implement a postorder traversal algorithm? (solution)
28) How do you traverse a binary tree in postorder traversal without recursion?
(solution)
29) How are all leaves of a binary search tree printed? (solution)
40) How do you count a number of leaf nodes in a given binary tree? (solution)
41) How do you perform a binary search in a given array? (solution)
Binary tree based questions sometimes get trick and if you are having trouble solving
these tree-based list coding questions then I suggest you revise your data structure
and algorithms skill by going through From 0 to 1: Data Structures & Algorithms in
Java course.
It's written by an ex-Googler and it is one of the most comprehensive course to revise
all important data structures like an array, linked list, binary tree etc.
23) Write a program to sort numbers in place using quick sort? (solution)
30) Write a function to compute Nth Fibonacci number? Both iterative and
recursive?
You can check this Java program to print Fibonacci Series using recursion and
iteration.
31) Write a program to find out if two rectangles R1 and R2 are overlapping?
32) You need to write a function to climb n steps you can climb either 1 step at a
time or 2 steps a time, write a function to return a number of ways to climb a ladder
with n step.
It's actually a Fibonacci series so you can solve it like that.
33) Write code for Generate Random No in a range from min to max?
34) Write a program for word-wrap which should work on any screen size?
35) Design an algorithm to find the frequency of occurrence of a word in an article?