Interview Algorithemdocx
Interview Algorithemdocx
com/
Array
Ease
1
4. Find two smallest elements in a given array
Objective: Given an array of integers, write an algorithm to
find the two smallest elements in the array.
Example:
2
Int[]a ={6, 8, 1, 9, 2, 1, 10}; Output: 10,9 Int [] a
= {6, 8, 1, 9, 2, 1, 10, 10}; Output: 10,10 Int [] a =
{6};Output: Invalid Input, array size is less than 2
int a [] = { 3,-1,-7,-4,-5,9,10};
3
Elements are -4 9 -5
4
15. Find the increasing OR decreasing point in an array
int
[]a={1,2,4,6,11,15,19,20,21,31,41,51,55,46,35,24,18,14,13,12,1
1,4,2,1}; output: 55
int [] a = {1,2,4}; //no deceasing element, so last element
will be answer output: 4
int [] a = {4,2,1}; //no deceasing element, so last element
will be answer output: 4
16. Find the only element in array which appears only once
Objective: Given an array of integers, all the elements are
appear twice but one element which appears only once. Write an
algorithm to find that element.
Example:
int [] a = { 1,5,6,2,1,6,4,3,2,5,3};
output: 4
5
18. Find the Kth Smallest/Largest Element in an Array
Objective: Given an array of integers. find the
Kth Smallest/largest element in the array.
21. Magic Index – Find Index In Sorted Array Such That A[i] = i.
Objective: Given a sorted array of distinct integers, Find the
Magic index or Fixed point in the array.
Magic Index or Fixed Point: Magic index or a Fixed point in an
array is an index i in the array such that A[i] = i.
Example :
6
level and elements at the level will be the Sum of consecutive
elements in the previous level. Print it in a reverse level.
See Example.
Example:
int []A={2,1,3,5,5,3,2,1,6,7,7,8,8};
Output : Missing duplicate is 6
7
25. Find intersection between Two Sorted Arrays.
Objective: Given two sorted arrays, Find intersection point
between them.
Examples:
8
Output: True or false based upon we have found any two numbers
in array arrA[] whose sum is equal to k
Intermediate
9
A [] = {1, 3, 5, 7, 9, 2, 4, 6, 8, 10}, n= 5 Output: {1, 2, 3,
4, 5, 6, 7, 8, 9, 10}
10
Example:
11
9. Kadane’s Algorithm – Maximum Subarray Problem
Objective: The maximum subarray problem is the task of
finding the contiguous subarray within a one-
dimensional array of numbers which has the largest sum.
12
12. In an Array, find the Contiguous Subarray with Sum to a Given
Value.
Objective: Given an array and an integer, find the Subarray
whose sum is equal to the given integer.
Examples:
int[] arrA = { 25, 12, 14, 22, 19, 15, 10, 23 }; Integer = 55
Output : 55 is found between indexes 2 and 4 And Elements are
: 14 22 19
13. In an Array, find the Smallest Subarray with Sum Greater than
the Given Value
Objective: Given an array and an integer, find the smallest
subarray whose sum is greater than the given integer.
Examples:
13
int[] arrA = { 12, 3, 1, 5, 6, 4, 10, 9, 8, 0 }; Output:
Max(j-i) where j>i and A[j]>A[i] is : 7
14
Singly
Input Array : 2 6 9 1 4 4 2 1 10 13 5 7 8
DefinedArray : 1 2 4 6 Output : 1 1 2 2 4 4 6 5 7 8 9 10 13
19. Sort an Array such that the odd numbers appear first followed
by the even numbers . The odd numbers in ascending order and
the even numbers in descending order.
Objective: Given an array of intergers, sort it such that the
odd numbers appear first followed by the even numbers . The
odd numbers in ascending order and the even numbers in
descending order.
Input: An Array of Integers
Example:
15
20. Find the number of occurrences of a number in a given sorted
array.
Objective: Given a sorted(ascending order) arrays of integers,
find out the number of occurences of a number in that array
Input: A sorted array arrA[] and a number x.
Output: number of occurrences of ‘x’ in array arrA[].
Examples :
16
Example:
23. Count All Paths from Top left to bottom right in Two
Dimensional Array including Diagonal Paths
Objective: Count all the paths from left top corner to right
bottom corner in two dimensional array.
Input: Two Dimensional array
Output: No of paths.
Approach :
1. Recursive
Recursive solution to this problem is similar to Print All
Paths from Top left to bottom right in Two Dimensional Array
But the Time complexity will be exponential because there will
be many sub problems which will be solved again and again to
get the final solution. read this : “Dynamic programming vs
Recursion
17
Example:
Print All Paths from Top left to bottom right in Two Dimensional Array
Objective: Print all the paths from left top corner to right
bottom corner in two dimensional array.
Input: Two Dimensional array
Output: Print all the paths.
Note: At the End of the article you will know what needs to be
included if you want to print the diagonal paths as well.
Example:
18
Print All Elements of Two Dimensional Array in Spiral
Objective: This question was asked in Amazon interview for the
Software development Engineer position, Write an algorithm to
print all the elements of two dimensional array in spiral.
Example :
19
Print 2D array in Spiral
Input: Two dimensional array
Output: All array elements printed in spiral.
20
Output : True or false based on whether element exists and its
location
Expert
2. Count and print all Subarrays with product less than K in O(n)
21
Objective: Given an array of positive integers and integer
‘K’. Write an algorithm to count all the possible sub arrays
where product of all the elements in the sub array is less
than k.
Example:
22
int [] a = {1, 2, 3}; Output: Possible subarrays – {1}, {2},
{3}, {1, 2} , {2, 3}, {1, 2, 3} So sum = 1+ 2+ 3 + 3 + 5 + 6
= 20
23
[1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 9, 9, 9, 10, 11, 14, 15, 16,
19]
1 2 3 1 2 4 1 2 5
1 3 4 1 3 5 1 4 5
2 3 4 2 3 5 2 4 5
3 4 5
int[] arrA = { 1, 2, -3, -4, -5, 6, -7, -8, 9, 10, -11, -12, -
13, 14 }; Output: -13 9 -3 10 -5 6 -7 2 -12 1 -11 14 -4 -8
11. Find All Elements in an Array which appears more than N/K
times, N is Array Size and k is a Number.
Objective: Given an array of size of N and number k. Find all
elements in an Array which appears more than N/K times.
Input: Array [] and number k.
Example:
24
int[] arrA = { 2, 2, 4, 4, 3, 5, 3, 4, 4, 6, 4, 3, 3, 8 }; K =
4 N/k = 14/4 = 3 Output will be [3,4] they appear 5, 4 times
respectively.
String
Easy
1. Find The Longest Sequence Of Prefix Shared By All The Words In A String
Objective: Write an algorithm to find The Longest Sequence of Prefix Shared by All the
Words in a String. This interesting problem was asked in the Google interview for software
engineer. This problem is bit tricky, it looks difficult but actually it is simple problem.
Input: A String
Output: The longest sequence of prefix common in all the words in a string
Example:
“Bedroom BedClock BedTable BedWall” => “Bed”
25
Example:
26
Input: “tutorial horizon” Output: “noziroh lairotut”
7. Check if two Strings are equal without using built-in function – Java
Objective– Given two strings, find out if they are equal or not without using any built-in
function (without using equals() function in java).
Example:
27
Input- tutorial horizon
Character: o has occurred max times: 3
----------------------
Input- abcabcdefabcab
Character: a has occurred max times: 4
Character: b has occurred max times: 4
13. String Compression using count of repeated characters – Run Length Encoding
Objective: Write an algorithm to compress the given string by using the count of repeated
characters and if new compressed string length is not smaller than the original string then
return the original string.
Example:
28
14. Find Whether Two Strings are Permutation of each other
Objective: Given Two Strings, check whether one string is permutation of other
Input: Two Strings
Output: True or false based on whether strings are permutation of other or not.
Example:
Intermediate
k = 2, n = 2
Output: 0 0 0 1 1 0 1 1
k = 3, n = 2
29
Output:
0 0 1 0 2 0 0 1 1 1 2 1
0 2 1 2 2 2
Example:
Character – A N = 6 Option 1:
30
Copy All – this will copy ‘A’
Paste – output “AA” Paste – output “AAA”
Paste – output “AAAA” Paste – output “AAAAA” Paste – output
“AAAAAA”
Total operations – 6
Option 2:
Copy All – this will copy ‘A’ Paste – output “AA” Paste –
output “AAA” Copy All Paste – output “AAAAAA”
Total operations – 5
Since with option 2, the task is done in 5 operations. Minimum
operations – 5
a a b a b c a b c d
b b c b c d
c c d
d
Approach:
31
8. Remove Duplicates from a string
Objective: Given a string, write an algorithm to remove the duplicate characters in that
string.
Example:
Input: tutorialhorizon
Output: tuorialhzn
Example :
32
Example: Input String : I am Sumit Jain
Output String : I%20am%20Sumit%20Jain
Expert
1. Text Justification Problem (OR Word Wrap Problem)
Objective: Given a list of words and length L. Format the words so that each line will have
only L characters and fully justified (left and right justified).
Restrictions-
Example:
33
N = 2, K = 3
[1, 1] [2, 1] [3, 1] [1, 2] [2, 2] [3, 2] [1, 3] [2, 3] [3, 3]
Given n=2
Output:
(())
()()
34