0% found this document useful (0 votes)
40 views21 pages

ZOHO questions

The document outlines a set of programming problems, each with specific input and output requirements. Problems include finding maximum subarray sums, detecting duplicates in arrays, merging sorted arrays, and validating IP addresses, among others. Each problem is accompanied by examples to illustrate the expected results.

Uploaded by

joykiruba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views21 pages

ZOHO questions

The document outlines a set of programming problems, each with specific input and output requirements. Problems include finding maximum subarray sums, detecting duplicates in arrays, merging sorted arrays, and validating IP addresses, among others. Each problem is accompanied by examples to illustrate the expected results.

Uploaded by

joykiruba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

SAMPLE QUESTION SET

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)

2. Find duplicates in an array

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.

3. Union of two arrays

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.

Note : Elements are not necessarily distinct.

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.

4. Finding middle element in a linked list

Given a singly linked list of N nodes.


The task is to find the middle of the linked list. For example, if the linked list is
1-> 2->3->4->5, then the middle node of the list is 3.
If there are two middle nodes(in case, when N is even), print the second middle element.
For example, if the linked list given is 1->2->3->4->5->6, then the middle node of the list
is 4.

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.

5. Reverse a linked list

Given a linked list of N nodes. The task is to reverse this list.

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.

7. Height of Binary Tree: Given a binary tree, find its height.


Example 1:

Input:
1
/ \
2 3
Output: 2

Example 2:
Input:
2
\
1
/
3
Output: 3

8.Validate an IP Address

Write a program to Validate an IPv4 Address.


According to Wikipedia, IPv4 addresses are canonically represented in dot-decimal notation,
which consists of four decimal numbers, each ranging from 0 to 255, separated by dots, e.g.,
172.16.254.1 .
A valid IPv4 Address is of the form x1.x2.x3.x4 where 0 <= (x1, x2, x3, x4) <= 255.
Thus, we can write the generalized form of an IPv4 address as (0-255).(0-255).(0-255).(0-
255).
Note: Here we are considering numbers only from 0 to 255 and any
additional leading zeroes will be considered invalid.

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

10. Rearrange Array Alternately

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.

11. The Celebrity Problem

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.

12. Merge Without Extra Space

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.

13. Frequencies of Limited Range Array Elements

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.

14. Frequencies of Limited Range Array Elements

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.

15. Remove duplicate elements from sorted Array

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}

16.Longest Palindrome in a String

Given a string S, find the longest palindromic substring in S. Substring of string S: S[ i . . . .


j ] where 0 ≤ i ≤ j < len(S). Palindrome string: A string which reads the same backwards.
More formally, S is palindrome if reverse(S) = S. Incase of conflict, return the substring
which occurs first ( with the least starting index).
Example 1:

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.

17.Total Decoding Messages

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:

Input: str = "123"


Output: 3
Explanation: "123" can be decoded as "ABC"(123),
"LC"(12 3) and "AW"(1 23).

Example 2:

Input: str = "90"


Output: 0
Explanation: "90" cannot be decoded as it's an invalid string and we cannot decode '0'.

18. Merge two sorted linked lists

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.

19. LRU Cache

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:

1. SET x y : sets the value of the key x with value y


2. GET x : gets the key of x if present else returns -1.

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

GET 1 : Print the value corresponding


to Key 1, ie 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

SET 2 3 : 1 -> 2, 2 -> 3 (the most recently


used one is kept at the rightmost position)

SET 1 5 : 2 -> 3, 1 -> 5

SET 4 5 : 1 -> 5, 4 -> 5 (Cache size is 2, hence


we delete the least recently used key-value pair)

SET 6 7 : 4 -> 5, 6 -> 7

GET 4 : Prints 5 (The cache now looks like


6 -> 7, 4->5)

SET 1 2 : 4 -> 5, 1 -> 2


(Cache size is 2, hence we delete the least
recently used key-value pair)

GET 3 : No key value pair having


key = 3. Hence, -1 is printed.

20.Element with left side smaller and right side greater

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.

22.Largest Number formed from an 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

24. Index Of an Extra Element

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

25. Sum of two large numbers

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.

You might also like