Zoho
Programming Test
Duration: 3 hours
Instructions
You can write programs in any programming language. Please download and use a local IDE
if possible.
There are 5 questions. You will be given questions one by one. Only after completing the
current question, next question will be given.
The questions are self-explanatory. Read the questions and understand them clearly before
you start writing the programs.
Test your program with the sample inputs along with other edge cases.
Program logic, efficiency, coding standards and modularity will be considered for
evaluation.
Hard code inputs (like int x = 10, int scores[] = {40, 20}).
Use only primitive data types. Don’t use any library functions.
Handle as many use cases as possible.
Once you have completed the program inform the evaluator, get it verified and get next
question.
Two Sum
Given an array of integers nums and an integer target, return indices of the two numbers such
that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the
same element twice.
You can return the answer in any order.
Example 1:
Input:
nums = [2,7,11,15], target = 9
Output:
[0,1]
Explanation:
Because nums[0] + nums[1] == 9, we return [0, 1].
Example 2:
Input:
nums = [3,2,4], target = 6
Output:
[1,2]
Example 3:
Input:
nums = [3,3], target = 6
Output:
[0,1]
Constraints:
2 <= nums.length <= 104
-109 <= nums[i] <= 109
-109 <= target <= 109
Only one valid answer exists.
2 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College
Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string "".
Example 1:
Input:
strs = ["flower","flow","flight"]
Output:
"fl"
Example 2:
Input:
strs = ["dog","racecar","car"]
Output:
""
Explanation:
There is no common prefix among the input strings.
Constraints:
1 <= strs.length <= 200
0 <= strs[i].length <= 200
strs[i] consists of only lowercase English letters.
B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College 3
Valid Parentheses
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string
is valid.
An input string is valid if:
1. Open brackets must be closed by the same type of brackets.
2. Open brackets must be closed in the correct order.
3. Every close bracket has a corresponding open bracket of the same type.
Example 1:
Input:
s = "()"
Output:
true
Example 2:
Input:
s = "()[]{}"
Output:
true
Example 3:
Input:
s = "(]"
Output:
false
Constraints:
1 <= s.length <= 104
s consists of parentheses only '()[]{}'.
4 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College
Plus One
You are given a large integer represented as an integer array digits, where each digits[i] is the
ith digit of the integer. The digits are ordered from most significant to least significant in left-to-
right order. The large integer does not contain any leading 0's.
Increment the large integer by one and return the resulting array of digits.
Example 1:
Input:
digits = [1,2,3]
Output:
[1,2,4]
Explanation:
The array represents the integer 123.
Incrementing by one gives 123 + 1 = 124.
Thus, the result should be [1,2,4].
Example 2:
Input:
digits = [4,3,2,1]
Output:
[4,3,2,2]
Explanation:
The array represents the integer 4321.
Incrementing by one gives 4321 + 1 = 4322.
Thus, the result should be [4,3,2,2].
Example 3:
Input:
digits = [9]
Output:
[1,0]
B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College 5
Explanation:
The array represents the integer 9.
Incrementing by one gives 9 + 1 = 10.
Thus, the result should be [1,0].
Constraints:
1 <= digits.length <= 100
0 <= digits[i] <= 9
digits does not contain any leading 0's.
6 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College
Climbing Stairs
You are climbing a staircase. It takes n steps to reach the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the
top?
Example 1:
Input:
n=2
Output:
Explanation:
There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps
Example 2:
Input:
n=3
Output:
Explanation:
There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step
Constraints:
1 <= n <= 45
B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College 7
Best Time to Buy and Sell Stock
You are given an array prices where prices[i] is the price of a given stock on the ith day.
You want to maximize your profit by choosing a single day to buy one stock and choosing a
different day in the future to sell that stock.
Return the maximum profit you can achieve from this transaction. If you cannot achieve any
profit, return 0.
Example 1:
Input:
prices = [7,1,5,3,6,4]
Output:
Explanation:
Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you
sell.
Example 2:
Input:
prices = [7,6,4,3,1]
Output:
Explanation:
In this case, no transactions are done and the max profit = 0.
Constraints:
1 <= prices.length <= 105
0 <= prices[i] <= 104
8 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College
Single Number
Given a non-empty array of integers nums, every element appears twice except for one. Find
that single one.
You must implement a solution with a linear runtime complexity and use only constant extra
space.
Example 1:
Input:
nums = [2,2,1]
Output:
Example 2:
Input:
nums = [4,1,2,1,2]
Output:
Example 3:
Input:
nums = [1]
Output:
Constraints:
1 <= nums.length <= 3 * 104
-3 * 104 <= nums[i] <= 3 * 104
Each element in the array appears twice except for one element which appears only once.
B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College 9
Move Zeroes
Given an integer array nums, move all 0's to the end of it while maintaining the relative order
of the non-zero elements.
Note that you must do this in-place without making a copy of the array.
Example 1:
Input:
nums = [0,1,0,3,12]
Output:
[1,3,12,0,0]
Example 2:
Input:
nums = [0]
Output:
[0]
Constraints:
1 <= nums.length <= 104
-231 <= nums[i] <= 231 - 1
10 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College
Find Winner on a Tic Tac Toe Game
Tic-tac-toe is played by two players A and B on a 3 x 3 grid. The rules of Tic-Tac-Toe are:
Players take turns placing characters into empty squares ' '.
The first player A always places 'X' characters, while the second player B always places 'O'
characters.
'X' and 'O' characters are always placed into empty squares, never on filled ones.
The game ends when there are three of the same (non-empty) character filling any row,
column, or diagonal.
The game also ends if all squares are non-empty.
No more moves can be played if the game is over.
Given a 2D integer array moves where moves[i] = [rowi, coli] indicates that the ith move will be
played on grid[rowi][coli]. return the winner of the game if it exists (A or B). In case the game
ends in a draw return "Draw". If there are still movements to play return "Pending".
You can assume that moves is valid (i.e., it follows the rules of Tic-Tac-Toe), the grid is initially
empty, and A will play first.
Example 1:
Input:
moves = [[0,0],[2,0],[1,1],[2,1],[2,2]]
Output:
"A"
Explanation:
A wins, they always play first.
B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College 11
Example 2:
Input:
moves = [[0,0],[1,1],[0,1],[0,2],[1,0],[2,0]]
Output:
"B"
Explanation:
B wins.
Example 3:
Input:
moves = [[0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]]
Output:
"Draw"
12 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College
Explanation:
The game ends in a draw since there are no moves to make.
Constraints:
1 <= moves.length <= 9
moves[i].length == 2
0 <= rowi, coli <= 2
There are no repeated elements on moves.
moves follow the rules of tic tac toe.
B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College 13
Sort Even and Odd Indices Independently
You are given a 0-indexed integer array nums. Rearrange the values of nums according to the
following rules:
1. Sort the values at odd indices of nums in non-increasing order.
o For example, if nums = [4,1,2,3] before this step, it becomes [4,3,2,1] after. The
values at odd indices 1 and 3 are sorted in non-increasing order.
2. Sort the values at even indices of nums in non-decreasing order.
o For example, if nums = [4,1,2,3] before this step, it becomes [2,1,4,3] after. The
values at even indices 0 and 2 are sorted in non-decreasing order.
Return the array formed after rearranging the values of nums.
Example 1:
Input:
nums = [4,1,2,3]
Output:
[2,3,4,1]
Explanation:
First, we sort the values present at odd indices (1 and 3) in non-increasing order.
So, nums changes from [4,1,2,3] to [4,3,2,1].
Next, we sort the values present at even indices (0 and 2) in non-decreasing order.
So, nums changes from [4,1,2,3] to [2,3,4,1].
Thus, the array formed after rearranging the values is [2,3,4,1].
Example 2:
Input:
nums = [2,1]
Output:
[2,1]
Explanation:
Since there is exactly one odd index and one even index, no rearrangement of values takes place.
The resultant array formed is [2,1], which is the same as the initial array.
Constraints:
1 <= nums.length <= 100
1 <= nums[i] <= 100
14 B.BHUVANESWARAN | AP (SG) | CSE | Rajalakshmi Engineering College