0% found this document useful (0 votes)
42 views

Arrays and Strings

Uploaded by

M. Murali
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Arrays and Strings

Uploaded by

M. Murali
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 19

FINDMIND Boot-Camp June 2014

Boot Camp Code List


Seria Points Problem Statement
l#
1. 5 Write a ‘C’ program to remove all the duplicate entries and
pile up all the unique entries to the top of the array, and
replace the blank entries with -1
2. 4 Write a ‘C’ program to swap all odd elements and all even
elements of an integer array of size ‘n’
3. 4 Write a ‘C’ program to reverse the given array of integers of
size ‘n’
4. 5 Given a sorted array and a number and element K. find K
nearest elements to the number in sorted array.
5. 5 Given an array of unsorted elements, find the minimum
difference between any 2 elements in the array.
6. 5 Given an input string, write a function that returns the
compressed string for the input string in INPLACE. (no extra
memory) (length of compressed string < = length of input
string)

For example, if the input string is “aaabcdeeee”, then the


function should return “a3b1c1d1e4?.
7. 5 Write a ‘C’ program to rotate a m*n matrix by 900
8. 8 Write a ‘C’ program to perform the following sorting
techniques

 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

in the matrix, we can move up/down/right/left. for example, if


the matrix[3][4] is

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

15. 4 Given an array of 1s and 0s which has all 1s first followed by


all 0s. Find the number of 0s. Count the number of zeroes in
the given array.

Examples:

Input: arr[] = {1, 1, 1, 1, 0, 0}


Output: 2

Page 2 of 19
FINDMIND Boot-Camp June 2014

Input: arr[] = {1, 0, 0, 0, 0}


Output: 4

Input: arr[] = {0, 0, 0}


Output: 3

Input: arr[] = {1, 1, 1, 1}


Output: 0

16. 7 Given an array arr[] of size n where every element is in range


from 0 to n-1. Rearrange the given array so
that arr[i] becomes arr[arr[i]]. This should be done
with O(1) extra space.

Examples:

Input: arr[] = {3, 2, 0, 1}


Output: arr[] = {1, 0, 3, 2}

Input: arr[] = {4, 0, 2, 1, 3}


Output: arr[] = {3, 4, 2, 0, 1}

Input: arr[] = {0, 1, 2, 3}


Output: arr[] = {0, 1, 2, 3}

If the extra space condition is removed, the question


becomes very easy. The main part of the question is to do it
without extra space.

17. 6 You are given an array of 0s and 1s in random order.


Segregate 0s on left side and 1s on right side of the array.
Traverse array only once.

Input array = [0, 1, 0, 1, 0, 0, 1, 1, 1, 0]


Output array = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]

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).

Write a function which takes an array and emits the majority


element (if it exists), otherwise prints NONE as follows:

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

First, i gave the obvious solution. I computed the product of


the whole array and then divided it by each element to get

Page 4 of 19
FINDMIND Boot-Camp June 2014

the resultant array.

But he asked me to do it without using the division operation.


After some cross questioning I gave the following solution.
Store the product of the left side elements for each integer in
an array L[].

For eg : Here , L[]= {1 , 1 , 2 , 6 }

Do the same for the right side elements.

Here R[] = { 24 , 12 , 4 , 1}

The multiply R[i] and L[i] to get the resultant array.


Complexity : O(n)

Finally 2 people were selected.

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

I did it in O(n). Then he asked me to write a more optimized


code.
36. 6
You are given an array whose each element represents the
height of the tower. The width of every tower is 1. It starts
raining. How much water is collected between the towers?
Eg. [1,5,3,7,2] – then answer is 2 units between towers 5 and
7.
Looks easy, but if you don’t observe well, then you might end
up with the wrong logic like I did at first. Also there are lots
of possible corner cases. Luckily I could identify them all.
37. 7
Given an array of integers, find an index such that if you split
the array into two parts the absolute value of the difference
between the sum of elements in both parts had to be
minimum. After giving him the logic, he changed it to split it
into 3 parts such that sum of elements in all of them are
equal. I had to code this one.
38. 7
Given an array, find the longest increasing subsequence of
size 3 with max product, all numbers are positive.
39. 6
Given a string, find the longest substring which is palindrome.
For example, if the given string is “forgeeksskeegfor”, the
output should be “geeksskeeg”. I have seen this question ,
but never thought about solution.
Same question i got in interview, I was very happy to get
solution, interview asked me optimize further. I could fix 1
improvement and he suggested 1 improvement.
40. 6 Find first non-repeating character in String
ex: geeksforgeeks: f
geeksforgeeksFirst:o
41. 7 Given an array of +ve as well as -ve numbers, find out
whether it is possible or not to convert it to 0 by
adding/subtracting operations on all the elements.

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

last element in LIS, then L(i) can be recursively written as.


L(i) = { 1 + Max ( L(j) ) } where j < i and arr[j] < arr[i] and if
there is no such j then L(i) = 1
To get LIS of a given array, we need to return max(L(i)) where
0<i<n
So the LIS problem has optimal substructure property as the
main problem can be solved using solutions to subproblems.

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.

For i = 0 to N-1, A[A[i]%N] += N. Return i with max A[i]. O(n)


time
46. 10 Implement 2 stacks in an array .( Algo + Code ) .
Follow up question ->What do we do if we want to change the
size of array dynamically.
-> Implement 3 stacks in an array .( Algo )
-> Implement K stacks in an array .( Algo )

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

was sorted? (Code)


49. 7
Rotate a 2D matrix by 90 degree, but here the matrix is stored
in 1D form
50. 7 Given a string input : aaabbccdeeabb output should be :
a3b2c2de2ab2
Challenge here is that we need to do it in place, without any
other string or data structure

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

Given an array of size n, the array contains numbers in range


from 0 to k-1 where k is a positive integer and k <= n. Find the
maximum repeating number in this array. For example,
let k be 10 the given array be arr[] = {1, 2, 2, 2, 0, 2, 0, 2, 3, 8,
0, 9, 2, 3}, the maximum repeating number would be 2.
Expected time complexity is O(n) and extra space allowed
is O(1). Modifications to array are allowed.

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).

A better approach is to create a count array of size k and


initialize all elements of count[] as 0. Iterate through all

Page 10 of 19
FINDMIND Boot-Camp June 2014

elements of input array, and for every element arr[i],


increment count[arr[i]]. Finally, iterate through count[] and
return the index with maximum value. This approach takes
O(n) time, but requires O(k) space.

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) Iterate though input array arr[], for every element arr[i],


increment arr[arr[i]%k] by k (arr[]becomes {2, 11, 11, 29, 11,
12, 1, 15 })

2) Find the maximum value in the modified array (maximum


value is 29). Index of the maximum value is the maximum
repeating element (index of 29 is 3).

3) If we want to get the original array back, we can iterate


through the array one more time and doarr[i] = arr[i] %
k where i varies from 0 to n-1.

53. 6 Given a 2D matrix, print all elements of the given matrix in


diagonal order. For example, consider the following 5 X 4
input matrix.

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20

Diagonal printing of the above matrix is

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.

Given an array of size N, it contains all numbers from 1 to


N-2 except one which is repeated twice. Find the repeated
number.

Given an array of size N, which contains number ranging


from 1 to N-1 has one missing and one repeating number.
Find repeated and missing number.

Given an array of sorted number of size N and contains


numbers in range 1 to N-1. Find the smallest missing
number.

Given an array of size N, find all duplicate numbers.

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

that matrix. constraints you can move in 3 directions from


one cell 1. forward , 2. down 3. diagonal . Find all teh
occurrence of all the word

forward means right (x+1,y)


down mean (x,y+1)
diagonal means (x+1,y+1)

it can be done with BFS. {search the no. of occurance of a


given word example "sachin" in the whole NxN matrix}

w | s | r | t | g | g|
a|a|c|h|i|n|
k|c|h|u|j|j|
o|h|i|n|y|q|

in this sachin can be found out 4 times.

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"

60. 7 Given an array say [0,1,2,3,5,6,7,11,12,14,20]


given a number say 5.
Now find the sum of elements which sum to 5
eg:2+3=5, 0+5=5 etc.
I guess the interviewer wanted all possible combinations eg
0+2+3=5, etc
61. 6
Given a number in an array form, Come up with an algorithm
to push all the zeros to the end.
Expectation : O(n) solution

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

in an array of n size, which contains duplicate elements in


minimum time and space complexity.

67. 10 Given an array having 16000 unique integers, each lying


within the range 1<x<20000, how do u sort it. U can load only
1000 numbers at a time in memory.

use bit vector to store each integer in single bit. 625 integers
are required to store 20000 numbers

int arr[625] = {0};

ex: to store data 2000


arr index = 2000/32 = 62
bit position = 2000%32 = 16

now make bit position 16 of array index 62 arr[62] to 1

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

lets arr[10] bit position 15 is set to 1 then this is equivalent to


data 10*32+15 = 335
68. 7 Is this code working fine ? if yes/no give reason ?
#include<iostream>
using namespace std;
struct node
{
int a;
int b;
};
typedef struct node Node;
void swap(void *a,void *b)

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

place as we have given that array has just enough space to


hold the expanded array.

example:

1)input: a1b1c1

output:abc

length of array will be shortened.

2)input: a2b2c2

output:aabbcc

length of array will be equal to given array.

3)input: a3b4

output:aaabbbb

length of array will be greater than given array.


72. 9
Given an array of 0s and 1s, find out:
1. all the subsequences where number of 0s = number of 1s
2. max length subsequence where number of 0s = number of
1s

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

(The example added the other constraint that the


whitespaces donot matter.)

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:

You have a 9 by 9 board. This board is divided into nine rows,


nine columns, and nine 3x3 blocks. In a solved puzzle, every
row, every column, and every 0 block has to contain each of
the digits from 1 to 9. This is an example of a solved puzzle:

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

You might also like