ZOHO-DAY 2 Set 2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

16.Given a number, convert it into the corresponding alphabet.

Constraints:
The input number should be a positive integer.
The input number should not exceed 18278 (26 * 26 * 26) to stay within the range of A
to ZZZ.

Testcase 1:
Input: 702
Output: ZZ

Test Case 2:
Input: 18278
Output: ZZZ

17.You are given a 0-indexed integer array nums consisting of 3 * n elements.

You are allowed to remove any subsequence of elements of size exactly n from nums.
The remaining 2 * n elements will be divided into two equal parts:

The first n elements belonging to the first part and their sum is sumfirst.
The next n elements belonging to the second part and their sum is sumsecond.
The difference in sums of the two parts is denoted as sumfirst - sumsecond.

For example, if sumfirst = 3 and sumsecond = 2, their difference is 1.


Similarly, if sumfirst = 2 and sumsecond = 3, their difference is -1.
Return the minimum difference possible between the sums of the two parts after the
removal of n elements.
Constraints:

nums.length == 3 * n
1 <= n <= 105
1 <= nums[i] <= 105

Testcase 1:
Input: nums = [3,1,2]
Output: -1
Explanation: Here, nums has 3 elements, so n = 1.
Thus we have to remove 1 element from nums and divide the array into two equal parts.
- If we remove nums[0] = 3, the array will be [1,2]. The difference in sums of the two
parts will be 1 - 2 = -1.
- If we remove nums[1] = 1, the array will be [3,2]. The difference in sums of the two
parts will be 3 - 2 = 1.
- If we remove nums[2] = 2, the array will be [3,1]. The difference in sums of the two
parts will be 3 - 1 = 2.
The minimum difference between sums of the two parts is min(-1,1,2) = -1.

Test Case 2:
Input: nums = [7,9,5,8,1,3]
Output: 1
Explanation: Here n = 2. So we must remove 2 elements and divide the remaining array
into two parts containing two elements each.
If we remove nums[2] = 5 and nums[3] = 8, the resultant array will be [7,9,1,3]. The
difference in sums will be (7+9) - (1+3) = 12.
To obtain the minimum difference, we should remove nums[1] = 9 and nums[4] = 1. The
resultant array becomes [7,5,8,3]. The difference in sums of the two parts is (7+5) -
(8+3) = 1.
It can be shown that it is not possible to obtain a difference smaller than 1.

18.Given an integer num, return three consecutive integers (as a sorted array) that sum
to num. If num cannot be expressed as the sum of three consecutive integers, return an
empty array.

Constraints:
0 <= num <= 1015

Testcase 1:
Input: num = 33
Output: [10,11,12]
Explanation: 33 can be expressed as 10 + 11 + 12 = 33.
10, 11, 12 are 3 consecutive integers, so we return [10, 11, 12].

Test Case 2:

Input: num = 4
Output: []
Explanation: There is no way to express 4 as the sum of 3 consecutive integers.
19.You are given an array time where time[i] denotes the time taken by the ith bus to
complete one trip.

Each bus can make multiple trips successively; that is, the next trip can start
immediately after completing the current trip. Also, each bus operates independently;
that is, the trips of one bus do not influence the trips of any other bus.

You are also given an integer totalTrips, which denotes the number of trips all buses
should make in total. Return the minimum time required for all buses to complete at
least totalTrips trips.
Constraints:

1 <= time.length <= 105


1 <= time[i], totalTrips <= 107

Testcase 1:

Input: time = [1,2,3], totalTrips = 5


Output: 3

Explanation:
- At time t = 1, the number of trips completed by each bus are [1,0,0].
The total number of trips completed is 1 + 0 + 0 = 1.
- At time t = 2, the number of trips completed by each bus are [2,1,0].
The total number of trips completed is 2 + 1 + 0 = 3.
- At time t = 3, the number of trips completed by each bus are [3,1,1].
The total number of trips completed is 3 + 1 + 1 = 5.
So the minimum time needed for all buses to complete at least 5 trips is 3

Test Case 2:
Example 2:

Input: time = [2], totalTrips = 1


Output: 2
Explanation:
There is only one bus, and it will complete its first trip at t = 2.
So the minimum time needed to complete 1 trip is 2.
20.You are given the root of a binary tree that consists of exactly 3 nodes: the root, its
left child, and its right child.

Return true if the value of the root is equal to the sum of the values of its two children, or
false otherwise.

Constraints:

The tree consists only of the root, its left child, and its right child.
-100 <= Node.val <= 100

Testcase 1:
Input: root = [10,4,6]
Output: true
Explanation: The values of the root, its left child, and its right child are 10, 4, and 6,
respectively.
10 is equal to 4 + 6, so we return true.

Test Case 2:
Input: root = [5,3,1]
Output: false
Explanation: The values of the root, its left child, and its right child are 5, 3, and 1,
respectively.
5 is not equal to 3 + 1, so we return false.

21.Write a C program that takes two arrays of integers as input from the user. The
program should then separate the odd numbers from the first array and store them in a
new array, and likewise, separate the even numbers from the second array and store
them in another new array. Finally, the program should print out both new arrays.

Constraints:
The size of each array should not exceed 100 elements.
The range of integers in each array should be from -1000 to 1000.
The input array elements should be integers.
The program should handle cases where both arrays have no odd or even numbers
gracefully.
Testcase 1:
Input:
First Array: {3, 8, 5, 12, 7}
Second Array: {4, 9, 2, 11, 6}
Output:
Odd Array: {3, 5, 7}
Even Array: {4, 8, 12, 2, 6}

Test Case 2:
Input:
First Array: {1, 2, 3, 4, 5}
Second Array: {6, 7, 8, 9, 10}

Output:
Odd Array: {1, 3, 5}
Even Array: {2, 4, 6, 8, 10}

22.Write a function to find the longest common prefix string amongst an array of strings

Constraints:

The array of strings should not be empty.


The strings in the array should consist of printable ASCII characters.
The length of each string in the array should be within a reasonable limit, for example,
between 1 and 100 characters

Testcase 1:
Input: ["flower", "flow", "flight"]
Output: "fl"

Test Case 2:
Input: ["apple", "app", "apricot"]
Output: "ap"

23.The scenario involves testing the candidate's ability to utilize recursion and
backtracking to solve a problem efficiently.The question is to find all possible subsets
(the power set) of a given set of distinct integers. For example, if the input set is [1, 2,
3], the power set would be [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]. Each subset can
be considered as a combination of including or excluding each element from the original
set.

Constraints:
The input set of distinct integers should not be empty.
The integers in the set should be within a specified range, for example, between -10^9
and 10^9.
The length of the input set should not exceed a certain limit, for example, 20 elements,
to ensure the efficiency of the solution

Testcase 1:
Input: [1,2,3]
Expected Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
Explanation: The power set of [1,2,3] includes all possible subsets, including the empty
set, subsets with single elements, subsets with two elements, and the full set.

Test Case 2:
Input: [0]
Expected Output: [[],[0]]
Explanation: The power set of [0] includes the empty set and the set itself.

24.You are given the root of a binary tree that consists of exactly 3 nodes: the root, its
left child, and its right child.

Return true if the value of the root is equal to the sum of the values of its two children, or
false otherwise.

Constraints:

The tree consists only of the root, its left child, and its right child.
-100 <= Node.val <= 100

Testcase 1:

Input: root = [10,4,6]


Output: true
Explanation: The values of the root, its left child, and its right child are 10, 4, and 6,
respectively.
10 is equal to 4 + 6, so we return true.

Testcase 2:
Input: root = [5,3,1]
Output: false
Explanation: The values of the root, its left child, and its right child are 5, 3, and 1,
respectively.
5 is not equal to 3 + 1, so we return false.

25.Given an odd length word which should be printed from the middle of the word. The
output should be in the following pattern.

Constraints :

The input word should have an odd length, as the task is to print from the middle of the
word.
The length of the word should be greater than 1 to allow for a meaningful output pattern.

Testcase 1:
Input: PROGRAM
Output:
G
GR
GRA
GRAM
GRAMP
GRAMPR
GRAMPRO

Test Case 2:
Input: COLOR
Output:
L
LO
LOR
LORC
LORCO

You might also like