ZOHO questions
ZOHO questions
1.Kadane's Algorithm
Given an array Arr[] of N integers. Find the contiguous sub-array(containing at least one
number) which has the maximum sum and return its sum.
Example 1:
Input:
N=5
Arr[] = {1,2,3,-2,5}
Output:
9
Explanation:
Max subarray sum is 9
of elements (1, 2, 3, -2, 5) which
is a contiguous subarray.
Example 2:
Input:
N=4
Arr[] = {-1,-2,-3,-4}
Output:
-1
Explanation:
Max subarray sum is -1
of element (-1)
Given an array a[] of size N which contains elements from 0 to N-1, you need to find all the
elements occurring more than once in the given array.
Note: The extra space is only for the array to be returned.
Try and perform all operations within the provided array.
Example 1:
Input:
N=4
a[] = {0,3,1,2}
Output: -1
Explanation: N=4 and all elements from 0
to (N-1 = 3) are present in the given
array. Therefore output is -1.
Example 2:
Input:
N=5
a[] = {2,3,1,2,3}
Output: 2 3
Explanation: 2 and 3 occur more than once
in the given array.
Given two arrays a[] and b[] of size n and m respectively. The task is to find the number of
elements in the union between these two arrays.
Union of the two arrays can be defined as the set containing distinct elements from both the
arrays. If there are repetitions, then only one occurrence of element should be printed in the
union.
Example 1:
Input:
53
12345
123
Output:
5
Explanation:
1, 2, 3, 4 and 5 are the
elements which comes in the union set
of both arrays. So count is 5.
Example 2:
Input:
62
85 25 1 32 54 6
85 2
Output:
7
Explanation:
85, 25, 1, 32, 54, 6, and
2 are the elements which comes in the
union set of both arrays. So count is 7.
Example 1:
Input:
LinkedList: 1->2->3->4->5
Output: 3
Explanation:
Middle of linked list is 3.
Example 2:
Input:
LinkedList: 2->4->6->7->5->1
Output: 7
Explanation:
Middle of linked list is 7.
Example 1:
Input:
LinkedList: 1->2->3->4->5->6
Output: 6 5 4 3 2 1
Explanation: After reversing the list,
elements are 6->5->4->3->2->1.
Example 2:
Input:
LinkedList: 2->7->8->9->10
Output: 10 9 8 7 2
Explanation: After reversing the list,
elements are 10->9->8->7->2.
6.Permutations of a given string
Given a string S. The task is to print all unique permutations of the given string in
lexicographically sorted order.
Example 1:
Input: ABC
Output:
ABC ACB BAC BCA CAB CBA
Explanation:
Given string ABC has permutations in 6
forms as ABC, ACB, BAC, BCA, CAB and CBA .
Example 2:
Input: ABSG
Output:
ABGS ABSG AGBS AGSB ASBG ASGB BAGS
BASG BGAS BGSA BSAG BSGA GABS GASB
GBAS GBSA GSAB GSBA SABG SAGB SBAG
SBGA SGAB SGBA
Explanation:
Given string ABSG has 24 permutations.
Input:
1
/ \
2 3
Output: 2
Example 2:
Input:
2
\
1
/
3
Output: 3
8.Validate an IP Address
Your task is to complete the function isValid which returns 1 if the given IPv4 address is
valid else returns 0. The function takes the IPv4 address as the only argument in the form of
string.
Example 1:
Input:
IPv4 address = 222.111.111.111
Output: 1
Explanation: Here, the IPv4 address is as
per the criteria mentioned and also all
four decimal numbers lies in the mentioned
range.
Example 2:
Input:
IPv4 address = 5555..555
Output: 0
Explanation: 5555..555 is not a valid
IPv4 address, as the middle two portions
are missing.
9. Key Pair
Given an array Arr of N positive integers and another number X. Determine whether or not
there exist two elements in Arr whose sum is exactly X.
Example 1:
Input:
N = 6, X = 16
Arr[] = {1, 4, 45, 6, 10, 8}
Output: Yes
Explanation: Arr[3] + Arr[4] = 6 + 10 = 16
Example 2:
Input:
N = 5, X = 10
Arr[] = {1, 2, 4, 3, 6}
Output: Yes
Explanation: Arr[2] + Arr[4] = 4 + 6 = 10
Given a sorted array of positive integers. Your task is to rearrange the array elements
alternatively i.e first element should be max value, second should be min value, third should
be second max, fourth should be second min and so on.
Note: Modify the original array itself. Do it without using any extra space. You do not have
to return anything.
Example 1:
Input:
n=6
arr[] = {1,2,3,4,5,6}
Output: 6 1 5 2 4 3
Explanation: Max element = 6, min = 1,
second max = 5, second min = 2, and
so on... Modified array is : 6 1 5 2 4 3.
Example 2:
Input:
n = 11
arr[]={10,20,30,40,50,60,70,80,90,100,110}
Output:110 10 100 20 90 30 80 40 70 50 60
Explanation: Max element = 110, min = 10,
second max = 100, second min = 20, and
so on... Modified array is :
110 10 100 20 90 30 80 40 70 50 60.
A celebrity is a person who is known to all but does not know anyone at a party. If you go to
a party of N people, find if there is a celebrity in the party or not.
A square NxN matrix M[][] is used to represent people at the party such that if an element of
row i and column j is set to 1 it means ith person knows jth person. Here M[i][i] will always
be 0.
Note: Follow 0 based indexing.
Follow Up: Can you optimize it to O(N)
Example 1:
Input:
N=3
M[][] = {{0 1 0},
{0 0 0},
{0 1 0}}
Output: 1
Explanation: 0th and 2nd person both
know 1. Therefore, 1 is the celebrity.
Example 2:
Input:
N=2
M[][] = {{0 1},
{1 0}}
Output: -1
Explanation: The two people at the party both
know each other. None of them is a celebrity.
Given two sorted arrays arr1[] and arr2[] of sizes n and m in non-decreasing order. Merge
them in sorted order without using any extra space. Modify arr1 so that it contains the first N
elements and modify arr2 so that it contains the last M elements.
Example 1:
Input:
n = 4, arr1[] = [1 3 5 7]
m = 5, arr2[] = [0 2 6 8 9]
Output:
arr1[] = [0 1 2 3]
arr2[] = [5 6 7 8 9]
Explanation:
After merging the two
non-decreasing arrays, we get,
0 1 2 3 5 6 7 8 9.
Example 2:
Input:
n = 2, arr1[] = [10, 12]
m = 3, arr2[] = [5 18 20]
Output:
arr1[] = [5 10]
arr2[] = [12 18 20]
Explanation:
After merging two sorted arrays
we get 5 10 12 18 20.
Given an array A[] of N positive integers which can contain integers from 1 to P where
elements can be repeated or can be absent from the array. Your task is to count the frequency
of all elements from 1 to N.
Note: The elements greater than N in the array can be ignored for counting and do modify
the array in-place.
Example 1:
Input:
N=5
arr[] = {2, 3, 2, 3, 5}
P=5
Output:
02201
Explanation:
Counting frequencies of each array element
We have:
1 occurring 0 times.
2 occurring 2 times.
3 occurring 2 times.
4 occurring 0 times.
5 occurring 1 time.
Example 2:
Input:
N=4
arr[] = {3,3,3,3}
P=3
Output:
0040
Explanation:
Counting frequencies of each array element
We have:
1 occurring 0 times.
2 occurring 0 times.
3 occurring 4 times.
4 occurring 0 times.
Given an array A[] of N positive integers which can contain integers from 1 to P where
elements can be repeated or can be absent from the array. Your task is to count the frequency
of all elements from 1 to N.
Note: The elements greater than N in the array can be ignored for counting and do modify
the array in-place.
Example 1:
Input:
N=5
arr[] = {2, 3, 2, 3, 5}
P=5
Output:
02201
Explanation:
Counting frequencies of each array element
We have:
1 occurring 0 times.
2 occurring 2 times.
3 occurring 2 times.
4 occurring 0 times.
5 occurring 1 time.
Example 2:
Input:
N=4
arr[] = {3,3,3,3}
P=3
Output:
0040
Explanation:
Counting frequencies of each array element
We have:
1 occurring 0 times.
2 occurring 0 times.
3 occurring 4 times.
4 occurring 0 times.
Given a sorted array A[] of size N, delete all the duplicated elements from A[]. Modify the
array such that if there are X distinct elements in it then the first X positions of the array
should be filled with them in increasing order and return the number of distinct elements in
the array.
Note:
1. Don't use set or HashMap to solve the problem.
2. You must return the number of distinct elements(X) in the array, the generated output will
print all the elements of the modified array from index 0 to X-1.
Example 1:
Input:
N=5
Array = {2, 2, 2, 2, 2}
Output: {2}
Explanation: After removing all the duplicates
only one instance of 2 will remain.
Example 2:
Input:
N=3
Array = {1, 2, 2}
Output: {1, 2}
Input:
S = "aaaabbaa"
Output: aabbaa
Explanation: The longest Palindromic
substring is "aabbaa".
Example 2:
Input:
S = "abc"
Output: a
Explanation: "a", "b" and "c" are the
longest palindromes with same length.
The result is the one with the least
starting index.
A top secret message containing letters from A-Z is being encoded to numbers using the
following mapping:
'A' -> 1
'B' -> 2
...
'Z' -> 26
You are an FBI agent. You have to determine the total number of ways that message can be
decoded, as the answer can be large return the answer modulo 109 + 7.
Note: An empty digit sequence is considered to have one decoding. It may be assumed that
the input contains valid digits from 0 to 9 and If there are leading 0s, extra trailing 0s and two
or more consecutive 0s then it is an invalid string.
Example 1:
Example 2:
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge
both of the list (in-place) and return head of the merged list.
Example 1:
Input:
N = 4, M = 3
valueN[] = {5,10,15,40}
valueM[] = {2,3,20}
Output: 2 3 5 10 15 20 40
Explanation: After merging the two linked
lists, we have merged list as 2, 3, 5,
10, 15, 20, 40.
Example 2:
Input:
N = 2, M = 2
valueN[] = {1,1}
valueM[] = {2,4}
Output:1 1 2 4
Explanation: After merging the given two
linked list , we have 1, 1, 2, 4 as
output.
Design a data structure that works like a LRU Cache. Here cap denotes the capacity of the
cache and Q denotes the number of queries. Query can be of two types:
The LRUCache class has two methods get() and set() which are defined as follows.
1. get(key) : returns the value of the key if it already exists in the cache otherwise
returns -1.
2. set(key, value) : if the key is already present, update its value. If not present, add the
key-value pair to the cache. If the cache reaches its capacity it should invalidate the
least recently used item before inserting the new item.
3. In the constructor of the class the capacity of the cache should be intitialized.
Example 1:
Input:
cap = 2
Q=2
Queries = SET 1 2 GET 1
Output: 2
Explanation:
Cache Size = 2
SET 1 2 GET 1
SET 1 2 : 1 -> 2
Example 2:
Input:
cap = 2
Q=8
Queries = SET 1 2 SET 2 3 SET 1 5
SET 4 5 SET 6 7 GET 4 SET 1 2 GET 3
Output: 5 -1
Explanation:
Cache Size = 2
SET 1 2 : 1 -> 2
Given an unsorted array of size N. Find the first element in array such that all of its left
elements are smaller and all right elements to it are greater than it.
Note: Left and right side elements can be equal to required element. And extreme elements
cannot be required element.
Example 1:
Input:
N=4
A[] = {4, 2, 5, 7}
Output:
5
Explanation:
Elements on left of 5 are smaller than 5
and on right of it are greater than 5.
Example 2:
Input:
N=3
A[] = {11, 9, 12}
Output:
-1
21.Number of occurrence
Given a sorted array Arr of size N and a number X, you need to find the number of
occurrences of X in Arr.
Example 1:
Input:
N = 7, X = 2
Arr[] = {1, 1, 2, 2, 2, 2, 3}
Output: 4
Explanation: 2 occurs 4 times in the
given array.
Example 2:
Input:
N = 7, X = 4
Arr[] = {1, 1, 2, 2, 2, 2, 3}
Output: 0
Explanation: 4 is not present in the
given array.
Given a list of non negative integers, arrange them in such a manner that they form the largest
number possible.The result is going to be very large, hence return the result in the form of a
string.
Example 1:
Input:
N=5
Arr[] = {3, 30, 34, 5, 9}
Output: 9534330
Explanation: Given numbers are {3, 30, 34,
5, 9}, the arrangement 9534330 gives the
largest value.
Example 2:
Input:
N=4
Arr[] = {54, 546, 548, 60}
Output: 6054854654
Explanation: Given numbers are {54, 546,
548, 60}, the arrangement 6054854654
gives the largest value.
23.Roman Number to Integer
Given a string in roman no format (s) your task is to convert it to an integer . Various
symbols and their values are given below.
I1
V5
X 10
L 50
C 100
D 500
M 1000
Example 1:
Input:
s=V
Output: 5
Example 2:
Input:
s = III
Output: 3
Given two sorted arrays of distinct elements. There is only 1 difference between the arrays.
First array has one element extra added in between. Find the index of the extra element.
Example 1:
Input:
N=7
A[] = {2,4,6,8,9,10,12}
B[] = {2,4,6,8,10,12}
Output: 4
Explanation: In the second array, 9 is
missing and it's index in the first array
is 4.
Example 2:
Input:
N=6
A[] = {3,5,7,9,11,13}
B[] = {3,5,7,11,13}
Output: 3
Given two strings denoting non-negative numbers X and Y. Calculate the sum of X and Y.
Example 1:
Input:
X = "25", Y = "23"
Output:
48
Explanation:
The sum of 25 and 23 is 48.
Example 2:
Input:
X = "2500", Y = "23"
Output:
2523
Explanation:
The sum of 2500 and 23 is 2523.