Arrays and Strings
Arrays and Strings
Bubble Radix
Quick Insertion
9. 5
Given an array that has positive numbers and negative
numbers and zero in it. You need to separate the negative
numbers and positive numbers in such a way that negative
numbers lies to left of zero and positive numbers to the right
5and the original order of elements should be maintained
10. 5 Given a matrix of characters and a string, find whether the
string can be obtained from the matrix. From each character
Page 1 of 19
FINDMIND Boot-Camp June 2014
ofas
llqw
zowk
and the string is follow, then the function should return true.
11. 7 Find the kth smallest element in a (MxN) matrix.
12. 7 Given a 2 D array. The rows and columns are sorted.
Find the kth largest element from the 2-d array in most
efficient way. Can it be done in-place
13. 5 Given an array of integers, {1,0,2,0,3,0,0,4,5,6,7,0,0,0}, you
have to create a new array which will be like
(1,2,3,4,5,6,7,0,0,0,0,0,0,0}, without using any other temporary
array.
14. 6 Given MxN matrix, which contains 1s and 0s only. Redraw the
matrix so that, if any one position [i,j] contains 1, mark the
entire row and column with 1. But make sure because of
newly marked 1s, don’t do the same
i/p o/p
00010 11111
00000 11011
00000 11011
10001 11111
01010 11111
Examples:
Page 2 of 19
FINDMIND Boot-Camp June 2014
Examples:
Page 3 of 19
FINDMIND Boot-Camp June 2014
18. 5
Write a program to print all the LEADERS in the array. An
element is leader if it is greater than all the elements to its
right side. And the rightmost element is always a leader. For
example int the array {16, 17, 4, 3, 5, 2}, leaders are 17, 5 and
2.
19. 6
You are given a list of n-1 integers and these integers are in
the range of 1 to n. There are no duplicates in list. One of the
integers is missing in the list. Write an efficient code to find
the missing integer
20. 5 Majority Element: A majority element in an array A[] of size n
is an element that appears more than n/2 times (and hence
there is at most one such element).
I/P : 3 3 4 2 4 4 2 4 4
O/P : 4
I/P : 3 3 4 2 4 4 2 4
O/P : NONE
21. 6
Given only putchar (no sprintf, itoa, etc.) write a routine
putlong that prints out an unsigned long int.
22. 6 Given an array of integers , replace each element with the
product of the remaining elements.
Eg : Input - 1 2 3 4
Output : 24 12 8 6
Page 4 of 19
FINDMIND Boot-Camp June 2014
Here R[] = { 24 , 12 , 4 , 1}
23. 7
Given a float number 7.64, convert it into the string WITHOUT
using any inbuilt function/library.
for eg:
input
float no.: 7.64
output
string: 7.64
24. 6
Given a string find the length of longest substring which has
none of its character repeated?
for eg:
i/p string:
abcabcbb
length of longest substring with no repeating charcters: 3
(abc)
25. 7
Find the longest increasing subsequence in O(nlogn). Proof
and full code was required.
Page 5 of 19
FINDMIND Boot-Camp June 2014
26. 6
Replace each element of an array with its greatest next
integer from the array in O(n).
27.
Given a 2D array containing only 0/1′s and each row is in
sorted order. Find the row which contains maximum number
of 1s.
28. 6
Given two strings, write a function to remove all characters in
one string which are present in other string
29. 6
Given 2 sorted arrays of size m and n+m(with n elements) ,
merge
them into the latter..
30. 7
Print a matrix in spiral order (Code)
Soln: Solved it using recursion. Each recursive call was
suppose to print boundary elements. On every recursive call,
shifted the origin point and passed new size of matrix.
31. 7
Given array of ints. find ar[i],ar[j] such that j>i and ar[j]-ar[i] is
maximum. Famous problem.
32. 6
Given an array of integers which is initially increasing and
then decreasing, find the maximum value in the array.
33. 7
Given an array containing both positive and negative
elements, arrange in such a manner — 1 positive number,
then 1 negative,then 1 positive and so on. If number of
negative numbers are more,extra numbers should be kept in
end and vice versa. Note the order of negative and positive
elements should be same in the modified array and you are
not allowed to use any extra space
34. 5
You have to count the number of first contiguous 0s of an
array. The size of array is not given.
35. 6
Given a sorted array which has been rotated, we have to find
the point of rotation.
Page 6 of 19
FINDMIND Boot-Camp June 2014
e.g arr[]={1,2,3}
Page 7 of 19
FINDMIND Boot-Camp June 2014
YES (1+2-3)
arr[]={3,6,2}
3+6-2 != 0
3-6-2 !=0
-3-6-2 !=0
-3-6+2 !=0
-3+6-2 !=0
-3+6+2 !=0
3-6+2 !=0
3+6+2 !=0
Hence ans= NO
Algorithm:
1) Have a variable called result initialized to zero.
2) Exor the absolute value of every input with the variable
result
3) Once all the inputs have been exored, the result must
contain 0 if it is possible for us to convert it to 0 by
adding/subtracting operations on
all the elements.
ELSE it has to be false..
42. 7
Given an array, find the longest increasing subset of size 3
with max product, all numbers are positive.
43. 8 Let us discuss Longest Increasing Subsequence (LIS) problem
as an example problem that can be solved using Dynamic
Programming.
The longest Increasing Subsequence (LIS) problem is to find
the length of the longest subsequence of a given sequence
such that all elements of the subsequence are sorted in
increasing order. For example, length of LIS for { 10, 22, 9, 33,
21, 50, 41, 60, 80 } is 6 and LIS is {10, 22, 33, 50, 60, 80}.
Optimal Substructure:
Let arr[0..n-1] be the input array and L(i) be the length of the
LIS till index i such that arr[i] is part of LIS and arr[i] is the
Page 8 of 19
FINDMIND Boot-Camp June 2014
Overlapping Subproblems:
Following is simple recursive implementation of the LIS
problem. The implementation simply follows the recursive
structure mentioned above. The value of lis ending with every
element is returned using max_ending_here. The overall lis is
returned using pointer to a variable max.
44. 7
Rotate a matix of dimensions M * N, by 900,, in place
45. 6 Given array of N integers ranging from 0 to N-1. Output
maximum repeating integer. Use only O(1) memory.
47. 6
Given an array of integers, print the 2 elements with least
absolute difference.
48. 7
Given an array where all numbers but one occurs in pairs,
suggest all ways to find the unique number. What if the array
Page 9 of 19
FINDMIND Boot-Camp June 2014
For both the problems full code with all the boundary
conditions was required
51. 6
Given a string. Find a character with most number of
occurrences. Write code and test case for same.
52. 6 Find the maximum repeating number in O(n) time and O(1) extra
space
The naive approach is to run two loops, the outer loop picks
an element one by one, the inner loop counts number of
occurrences of the picked element. Finally return the element
with maximum count. Time complexity of this approach
is O(n^2).
Page 10 of 19
FINDMIND Boot-Camp June 2014
Following is the O(n) time and O(1) extra space approach. Let
us understand the approach with a simple example
where arr[] = {2, 3, 3, 5, 3, 4, 1, 7}, k = 8, n = 8 (number of
elements in arr[]).
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
Page 11 of 19
FINDMIND Boot-Camp June 2014
1
5 2
9 6 3
13 10 7 4
17 14 11 8
18 15 12
19 16
20
54. 7 Given an integer array such that every element occurs 3 times except
one element which occurs only once, how do I find that single element
in O(1) space and O(n) time complexity?
55. 7 Given an array arrange it in the form a<b>c<d>e<f>g……….
56. 7 Given an array of numbers arrange it in the form of a single
number such that the concluded number is
the maximum.
e.g. given : 99,8,76,45,66,9,7,33,5,42
O/P : 99987766654233
57. 8 Give an array of size N, it contains all numbers from 1 to N-
1 except one. Find the missing number.
58. 10
given a N x N matrix find the no. of times a word is present in
Page 12 of 19
FINDMIND Boot-Camp June 2014
w | s | r | t | g | g|
a|a|c|h|i|n|
k|c|h|u|j|j|
o|h|i|n|y|q|
59. 9
Given a string say "I am a human being" the output should
reverse all letters of each word but not the whole string as
such.
Eg: O/p should be "I ma a namuh gnieb"
Page 13 of 19
FINDMIND Boot-Camp June 2014
62. 7
Give 2 arrays of size 7 and 3 which are sorted such that the
last 3 blocks in first array are empty, merge the arrays in a
sorted manner in the most efficient way.
E.g:-
a[7] = [4, 10, 11, 20__, __, __]
b[3] = [1,3,7]
63. 6
Explain the output of the following code:
int main( )
{
int x = 10, y = 20;/.
x =!x;
y =!x&&!y;
printf(“x =%d y =%d”, x, y);
return 0;
}
64. 10
Imagine we have a large string like this
"ABCBAHELLOHOWRACECARAREYOUIAMAIDOINGGOOD"
which contains multiple palindromes within it, like ABCBA,
RACECAR, ARA, IAMAI etc... Now write a method which will
accept this large string and return the largest palindrome
from this string. If there are two palindromes which are of
same size, it would be sufficient to just return any one of
them.
65. 8
How do you write a program which produces its own source
code as output?
66. 8
Find the majority element which occurs more than n/2 times
Page 14 of 19
FINDMIND Boot-Camp June 2014
use bit vector to store each integer in single bit. 625 integers
are required to store 20000 numbers
using this bit vectors we can store data range from 1 - 20000
in 625 integers..
after storing all data in bit vectors, check all bit positions of
arr[0], arr[1]..... arr[625]... if any bit position is set to 1 then
store back that data into array
Page 15 of 19
FINDMIND Boot-Camp June 2014
{
void *temp;
temp=a;
a=b;
b=temp;
}
int main()
{
Node *a1,*b1;
a1=(Node*)malloc(sizeof(Node));
b1=(Node*)malloc(sizeof(Node));
a1->a=10;
a1->b=20;
b1->a=30;
b1->b=40;
cout<<a1->a<<" "<<a1->b<<endl;
cout<<b1->a<<" "<<b1->b<<endl;
swap(a1,b1);
cout<<a1->a<<" "<<a1->b<<endl;
cout<<b1->a<<" "<<b1->b;
}
I surprised to see the ans after compiling this code ... i think u
enjoy this code... :)
69. 9 In an interview I was asked a question on strings. The
problem is given a string s1= "ABCDBCCDABCD". and a
pattern "BC". we have to replace this pattern with other
string ("UVW" or "U"or "uv"). Do this without creating new
string.
Take the case to replace "BC" with following
a) "uvw" s1=AUVWDUVWCDAUVWD .
b) "U" s1=AUDUCDAUD .
c) "UV" s1=AUVDUVCDAUVD .
70. 10 Write a program in C to read all the characters from standard
input and output the reverse when the user presses enter
key.
71. 10 we have given a char array like “a1b2c3″ we have to convert
this array to array like this “abbccc” .This has to be done in
Page 16 of 19
FINDMIND Boot-Camp June 2014
example:
1)input: a1b1c1
output:abc
2)input: a2b2c2
output:aabbcc
3)input: a3b4
output:aaabbbb
Update:
We need to find subarrays, not subsequences. Sorry for the
confusion.
73. 10
Given two strings find if they are anagrams or not.
eg.
"tom marvolo riddle" and "i am lord voldemort".
Page 17 of 19
FINDMIND Boot-Camp June 2014
74. 7
Give a N*N matrix, print it out diagonally.
Follow up, if it is a M*N matrix, how to print it out.
Example:
123
456
789
print:
1
24
357
68
9
75. 10
Given an array of (unsorted) integers, arrange them such that
a < b > c < d > e... etc.
76. 9
Implement a sudoku solution verifier function. The rules for
sudoku is this:
248|395|716
571|628|349
936|741|582
---+---+---
682|539|174
359|174|628
Page 18 of 19
FINDMIND Boot-Camp June 2014
714|862|953
---+---+---
863|417|295
195|286|437
427|953|861
77. 8
You are given an array, divide it into 2 equal halves such that
the sum of those 2 halves are equal. (Imagine that such
division is possible for the input array and array size is even)
Page 19 of 19