Google Online Assessment Questions
“Previous OA questions asked by Google”
OA 1:
There were 2 questions
(1) The Largest Element
We are supposed to find out the absolute difference of these distinct values, and since the absolute
difference must be within the range of min(arr) and max(arr). If we want to retrieve the Kth largest
element, we can do max(arr)-K if K is less than max(arr).
Question 2:
Didn't have enough time to figure out the solution (I'm not too good at solving graph/dp questions
also), was trying to do backtracking but probably will get TLE as well, felt like I've seen this question
somewhere before but still wasn't able to solve it. Would be great if someone can give me some
directions for this question.
OA 2:
Question 2:
You have n items, cost is c(i) and delivery cost is d(i). If customer orders more than one item, then
they get it for the minimum delivery cost. How do you find the maximum amount of money you can
make after delivering m items? I know this is a knapsack problem, but I just can't find a way to solve
it. And I want to make sure I am able to solve these problems for future interviews.
For e.g.
cost and delivery cost respectively (first column being cost, second being delivery cost):
Item 1: 7, 10
Item 2: 4, 15
Item3: 8, 1
m=2
Input format (java): int n, int m, int[][] arr where n is the total number of items, m is the maximum
number of items you can deliver, arr has each row with first element being cost, second being
delivery cost.
Output: 31 (You choose the first two items because if you chose the 3rd item, the delivery cost for 2
items would be 1 + 1 (since 1 is the mimimum delivery cost) so you would end up with 23 + 2 = 25.
OA 3:
Given an array of numbers A and two integers S and D, you can perform three operations
1. x + A[i]
2. x - A[i]
3. x xor A[i] (bitwise xor)
Each of these operations cost one move. Task is to find the minimum number of moves in
which S can be changed to D or return -1 otherwise.
Sample TC:
A = [6, 2, 7, 7]
S = 10
D = 21
Output: 3
Explanation:
10 + A[3] = 17
17 + A[1] = 19
19 + A[1] = 21
Constraints:
x lies in [0, 100000]
1 <= A.size() <= 1000
-10^9 <= A[i] <= 10^9
OA 4:
Given two arrays A and B of size N, that represents 2 rows of N tiles each.
Assuming 0 based indexing, you can start from the first tile of either row A or B and your goal is to
reach the last tile of either row i.e the terminating position must be A[n-1] or B[n-1].
You are supposed to end either row A or B through a series of jumps. You can jump from one tile to
another tile, that is at most C distance away from the current tile, in either row.
if you are at tile i of either of the 2 rows, in 1 move you can jump to any of the tiles from index i+1 to
i+C(both inclusive) of any row. Let's call this index J.
EVery tile has a cost associated with it, which is added to your total cost to use that tile. Depending
on the row you choose to start with, the cost of the tile A[0] or B[0] is initially added, and then
depending on the jumps made, the cost of the chosen tile is added.
There is one small problem though, if you jump from a tile in row A to tile in row B or vice-versa, an
additional cost od D is incurred for changing rows(i.e if you jump from A[i] to any element from B[i+1]
to B[i+c] or form B[i] to any element from A[i+1] to A[i+c]).
if you make jump in same array, no extra cost will be incurred(i.e jumping from A[i] to any element
from A[i+1] to A[i+c] or from B[i] to any element from B[i+1] to B[i+c]).
Find the minimum cost needed to reach from the starting tile to ending tile.
NOTE: As the answer can be very large, return your answer MOD 10^9+7.
Input 1-:
A = [1,2,3,4]
B = [3,2,1,1]
C=2
D=2
Input 2-:
A = [1,2,30,400]
B = [30,200,1,1]
C=2
D=2
o/p 1 - 5
o/p 2 - 5
Explanation 1:
One of the optimal path is:
B[0] -> B[2] -> B[3]
3+1+1=5
Explanation 2 -:
Optimal Path is:
A[0] -> B[2] -> B[3]
1 + (1+2(for changing row)) + 1 = 5
OA 5:
You are given the following:
● Integers n, c, d
● Array a as {a1, a2, ..., an} of length n
● Array b as {b1, b2, ..., bn} of length n
Task
Determine the number of pairs i, j (1 <= i < j <= n), satisfying the inequality
ai - aj + c <= bi - bj + d
Constraints
1 <= T <= 10
2 <= n <= 2.10^5
0 <= c, d <= 100
1 <= ai, bi <= 10^9
My approach
Make an array diff where diff[i] = a[i] - b[i]
Traverse on array a
● x = a[i] - b[i] + c - d
● check if x is less than (min-max) of diff, then, every pair is greater than x
● check if x is more than (max-min) of diff, then no pair is greater than x
● otherwise go check for i+1 to n
OA 6:
Question 1
Charlie has a magic mirror. The mirror shows right rotated versions of a given word. To generate
different right-rotations of a word, write the word in a circle in clockwise order, then start reading
from any given character in clockwise order till you have covered all the characters.
For example: In the word "sample",
If we start with 'p', we get the right rotated word as "plesam". There are six such right rotations
of "sample" including itself.
The inputs to the function isSameReflection consists of two strings, word1 and word2.The
function returns 1 if word1 and word2 are right rotations of the same word and -1 if they are not.
Both word1 and word? will strictly contain characters between 'a' -'z' (lower case letters)
Useful commands
strien() is used to calculate
Question 2
An agent sends a secret message to headquarters containing the details
of his project. He sends one soft copy to the agency's computer (P) and sends one hard copy
by fax to Roger, the technical head of the agency (Q). But during the
transmission, noise in the network causes some bits of the data message P to get distorted.
However, we know that Roger always matches the binary values of both messages
and checks whether he can convert the message P to message Q by flipping the minimum
number bits.
Write an algorithm to help Roger find the minimum number of bits that must be flipped to
convert message P to message Q
Input
The first line of the input consists of an integer numi, representing the secret message sent to
the agengy computer (P).
The second line consists of an integer num2, representing the message sent to the technical
hearof the agency (Q).
Output
Print an integer representing the minimum number of bits that must
Question 3
The first line of the input consists of an integer num1, representing the secret message sent to
the agency's
computer (P). The second line consists of an integer num2, representing the message sent to
the technical head
of the agency (Q).
Output
Print an integer representing the minimum number of bits that must be flipped to convert
message P to message Q.
Constraints
-104 s numt, num2s 10°
Example
Input:
7
10
Output:
Explanation:
Binary representation of P is 00000111.
Binary representation of Q is 00001010
Three bits of P at position 5, 6 and 8
must be flipped.
So, the number of bits that must be
flipped is 3.
Question 4
On a social networking site, each user can have a group of friends. Each user possesses a
unique profile ID. A company wants to promote its
product on the social networking site In a particular way. It plans to give rewards to any user
who promotes its product on his her wall. The company will give extra reward points to users
who refer other
users. The company will ask one of the users to promote its product by posting the product
message on his/her wall. The user can then share this message with their friends, asking them
to post on their walls as
well. The company will share the promo message with the user in such a way that the promo
message is posted on the maximum number of walls. Write an algorithm to help the company
find the userlD of the user
to whom they should send the promo request so that the request may reach the maximum
number of walls.
Input
The first line of the input consists of an integers users, representing the number of users[N) The
second line consist of two space separated integer numQ
OA 7:
Question 1
Balanced brackets
You are given a string S of length M. It consists of only two
characters;
● (: Opening bracket
● ): Closing bracket
S is a balanced bracket sequence. Formally, a sequence of brackets is balanced-if the following
conditions are met:
● It contains no unmatched brackets.
● The subset of brackets enclosed within the confines of a matched pair of
brackets is also a matched pair of brackets.
You are required to insert the character '+' in S such that there exists at least one '+' enclosed
within each pair of matched
bracket sequence. Find the minimum number of ' characters to be inserted in S to satisfy the
provided condition.
Input format
● The first line contains an integer T denoting the number of test cases. For each
test case :-
● The of first line of each test case contains an integer -N denoting the length of
string S
● The next line of each test case contains a string S.
Output format
For each test case. print the minimum number of characters that must be
be inserted in string S in a new line.
Constraints
1 ≤ T < 10
1≤ N ≤ 10
N is even
Sample Input Sample Output
1 2
6
()(())
Question 2
Maximize equal numbers
<problem>390</problem>
You are given the following:
● An array a consisting of n elements
● Integer k
For each (1 ≤ i ≤ n), perform the following operation exactly one
time:
● Replace a, by a, + x where z € [-k, k) which denotes should lie in the range of -k
and k, both inclusive.
Task
Determine the maximum length of the subsequence of array a, such that all numbers in that
subsequence are equal after applying the given operation.
Notes
● A subsequence of array a is an array that can be derived from array a by
removing some elements from it. (maybenone or all of them)
● Assume 1 based indexing
Example
Assumptions
● n=4
● k=1
● Array a = [2, 5, 1, 2]
Approach
Applying one operation on indices 1, 2, and 4, with x = 0 to get
array a as [2, 5, 1, 2].
Applying one operation on index 3, with x = 1 to get array a as [2, 5, 2, 2]
Therefore, the maximum length of the subsequence with equal numbers is 3.
Function description
Complete the Maximize qualNumbers function provided in the editor. This function takes the
following 3 parameters and returns the maximum length of the subsequence:
● n. Represents the size of array a
● k. Represents the integer k
● a. Represents the array a of length n
Input format
Note: This is the input format that you must use to provide custom input (available above the
Compile and Test button).
0 ≤ k ≤ 109
1 ≤ ai ≤ 109
Sample Input Sample Output
2 2
40 2
2256
32
156
Explanation
The first line represents the number of test cases
For test case 1:
Applying one operation with x - 0 on index 1, 2, 3, and 4 to get array
as [2, 2,5, 6]
Question 3
Diagonal number
Consider a square matrix of size N sorted diagonally (bottom left to the top right). The
diagonals in the matrix are numbered and filled with integer values from 1 to N2 as given below.
For example, consider a square matrix of size N = 3 then we have
the following matrix. You are given Q queries
and each query has an Integer x.
Task
Determine the diagonal number to which x belongs.
Notes
● The matrix is filled diagonally (bottom left to the top right). A diagonal is filled only
all the previous diagonals are filled
● The diagonals are filled starting with integer value 1 and so on
Input Format
Note: This is the input format that you must use to provide custom input (available above the
Compile and Test button).
• The first line contains an integer N denoting the size of the matrix.
• The next line contains an integer Q denoting the number queries.
• The next line contains Q space-separated integers denoting x for each query.
Output format
For each query, print a single integer in a new line representing
the diagonal number to which x belongs.
Constraints
● 1 ≤ N ≤ 106
● 1 ≤ Q ≤ 106
● 1 ≤ X ≤ N2
Sample Input Sample Output
5 1
3 6
1 16 25 9
Explanation
Consider a square matrix of size N = 5
For Query 1,1 occurs in the diagonal numbered 1
For Query 2,16 occurs in the diagonal numbered 6
For Query 3,25 occurs in the diagonal numbered 9
OA 8:
Question 2
OA 9:
Question 1
<problem>112</problem>
Given an array A of integers and an integer m.
We can perform following operation of the array :-
Choose an index i and change Ai to max(0,A[i]-k)
Find the minimum value of k such that each element is changed to 0 and number of operations
are atmost m.
Constraints :
1<=N<=10^5
0<=A[i]<=10^13
m<=10^9
Question 2
<problem>71</problem>
Given a list of strings S. Each string in S consist of characters from A-J. Find the minimum sum
of encoded strings. Encoding is a mapping from character(A-J) to unique integer(0-9).
eg : "ABC" represents number 102 if
'A' -> 1
'B' -> 0
'C' -> 2
Sample test case :
S = ["BA","A"]
Answer:
Explanation:
'A' -> 1 and 'B' -> 0
Therefore "BA" -> 1 and "A" -> 1
Constraints:
Sum of length of strings is atmost 10^5
Question 3
<problem>89</problem>
Find minimum X such that ((A|X)&(B|X)) == C given A, B, and C, if exists otherwise return -1.
Question 4
<problem>113</problem>
Given a binary array of 0s/1s count all subarrays having atleast one 0.
Also maximum one operation is allowed to convert 1 => 0 for any element in the given array
such that the number of subarrays with at least one 0 is maximized.
Bonus Onsite: Question 5
Given a k * k matrix with only 0 and 1, we can only move on cells of 1. Can we flip only one cell
from 1 to 0, so that no path is possible from (0,0) to (k-1,k-1).
Output should be yes or no
OA 10:
Question 2
OA 11:
Question 1
There N runners and M reports. report i says that runner A beats runner B. Return the order of
runners and how they finished There can be test cases like this: a->b, b->a
Example 1: A->B B->C A->C C->D E->D
Result: A,B,C,E,D
Example 2: a->b b->a
Result: Impossible
Question 2
<problem>32</problem>
Given a tree of N vertices and an edge E, calculate the number of vertices in each of the
connected components remaining after removal of the edge.
Test case: image
1,3 Result: 3,8
Question 3
Given a string containing only parentheses, scan it to see if the parentheses are well-nested,
then:
If they are, return the string as-is. If they are not, repair the string so that the parentheses are
well nested and return the result action can be: deleting, adding, changing
You must do string well-nested with minimum number of actions
Example: Input: (() Output: (()), or () or ()()
Input: (()))) Output: ((()))
OA 12:
OA 11:
Problem - 1
You are given a matrix M with n rows and m columns. The matrix only consists of either 0 or 11.
A right-angled isosceles triangle is defined as:
The hypotenuse has to be parallel to the x-axis.
The length of hypotenuse should be odd and greater than or equal to thredy
All the cells on and inside the triangle should be 0.
Task
Determine the total number of right-angled isosceles triangles, which are formed by 0.
Example
Assumptions
Approach
You can see that there is one right-angled isosceles triangle, and the length of the hypotenuse is
3, as shown in the following picture:
Input format
Note: This is the input format you must use to provide custom input (available above the
Compile and Test button).
• The first line contains one integer T denoting the number of test cases. T also specifies the
number of times you have to run
the find_triangles function on a different set of inputs.
For each test case:
• The first line contains an integer n representing the number of rows of the matrix.
• The second line contains an integer m representing the number of columns of the matrix.
• The next n lines contain a string of length m representing a row.
Output format
For each test case, print an integer in a new line representing the number of right-angled
isosceles triangles.
Constraints
1 ≤ T ≤ 20
1 ≤ n, m < 103
M[i][j] = 0 or1, 0 ≤ i<n, 0 ≤ j<m
Sample Input Sample Output
2 0
3 1
11111
01010
1111
1011
Explanation
The first line states the number of test cases. Therefore T=2
The first test case
You can clearly see that there is no isosceles right-angled triangle in the matrix
Therefore, the answer is O
The second test case
It is explained in the example above
OA 12:
Rackon String
OA 13:
Summary
● 2 problems, 1 hour
● Almost all languages allowed
Problem 1
You are given an array arr containing N distinct integers . You must perform the following
operations on this array.
1. For each pair of integers of the array, you must find the absolute difference D between
these integers and insert D into the array. if D is already an element of the array, then
you are not required to insert it.
2. You repeat task 1 until the array cannot be modified further.
Print Kth largest element of final array . If there is no such element print -1. Input format The first
line contains T denoting the number of test cases. The first line of each test case contains one
integer N The second line of each test case contains N space-separated integers denoting the
elements of the array arr. The third line of each test case cointains one integer K. Output format
For each test case , print Kth largest element of the final array in new line . If there is no such
element , then print -1. constraints 1 <= T <= 100 1 <= N,K <= 100000 1 <= arri <= 1000000
Example Input 2 5 1 3 5 6 7 2 1 1 2 Example Output 6 -1
Problem 2
You have N items, cost is c(i) and delivery cost is d(i). If customer orders more than one item,
then they get each item for the minimum delivery cost. How do you find the maximum amount of
money you can make after delivering atmost M items?
Constraints :
1 <= N <= 105
1 <= c(i) <= 105
1 <= d(i) <= 105
1 <= M <= N <= 105
Input Format :
First Line contains two numbers N and M
Next M lines contains two space seperated integers c(i) and d(i)
Output Format :
Single integer denoting the maximum amount of money that can be made
Sample Input
32
7 10
4 15
81
Sample Output: 31
Here you can choose first two items
Total cost = 7+4 = 11
Total delivery cost = 10*2 = 20
Total amount of money = 20+11 = 31
OA 14:
Summary
● 2 problems, 1 hour
● Almost all languages allowed
Problem 1
You are given n numbers a1,a2...an . For every index i you are required to select aj(1 <= j <= n , i
!= j ) in such a way that there exists a non-negative integer x satisfying the following condition
(a[i] | a[j]) + 1 = 2x where | denotes bitwise OR operation between a[i] and a[j].
Note
If no such aj exists, print -1.
If multiple answers exist, print the smallest aj
Input format
The first line contains an integer n denoting the number of integers in the list
The next line contains n space seperated integers denoting the numbers a1,a2,...an
Output format
If no such aj exists print -1 , if multiple answers exist, print the smallest aj.
Constraints
1 <= n <= 5*104
1 <= ai < 1024
1 <= i <= n
1 <= j <= n
j!=i
Sample input
4
2 13 19 5
Sample output
5 2 13 2
Explanation
For 2,(2|5) +1 = 8(23), (2|13) + 1 = 16(24) . You are required to print the smallest one . Therefore
the answer is 5.
For 13, (13|2) + 1 = 16(24)
For 19, (19|13) + 1 = 32(25)
For 5, (5|2) + 1 = 8(23)
Problem 2
OA 15:
Summary
● 2 problems, 1 hour
● Almost all languages allowed
Problem 1
You have to perform 3 types of queries on a set of numbers
1. 0 X add a number X
2. 1 X remove the number X (X always exist)
3. 2 X return the number of subsets that sum to X
Constraints : 0 <= X <= 103 0 <= number of queries <= 103
Problem 2
You are given N balls and they move either in right or left direction and for balls moving in
opposite direction, collision takes place and the one with less velocity(magnitude) gets
destroyed . In case if both have equal magnitude both gets destroyed. Negative velocity
represents moving towards left and positive velocity represents moving towards right . Print the
indices of balls in ascending order that remains undestroyed after all collisions.
Constraints:
1 <= N <= 105
-105 <= velocity <= 105 (velocity != 0)
Input: Velocities = [5,10,-5]
Output: [1,2]
Explanation: Balls 2 and 3 collides and ball 3 gets destroyed . Balls 1 and 2 never collide.
Input: Velocities = [8,-8]
Output: []
Explanation: Balls 1 and 2 collide destroying each other.
OA 15:
OA 16:
Problem - 1
Finding sum of integers
You are given an integer A. Consider two integers A and A + 1. Print the sum of the numbers
that cannot be formed using any combination of A
and A + 1. Since the answer can be large, print the sum modulo 109 + 7.
A combination of A and A + 1 is represented as integer Z that can be denoted as x × A + y × (A
+ 1) where both r and y are integers and
x, y ≥ 0.
Input format
The first line contains T denoting the number of
test cases.
Each of the T lines contains a single integer A.
Output format
For each test case, print the sum of the numbers that cannot be formed using any combination
of 4 and A + 1 in a new line. Since the answer can
be large, print the sum modulo 109 + 7.
Constraints
1 ≤ T < 103
1< A < 104
Sample Input Sample Output
3 1
2 8
3 30
Explanation
For A= 2, we can generate all numbers with combination of and A + 1 except 1 So, answer is 1
For A = 4, we can generate all numbers with combination of A and A + 1 except 1, 2, 3, 6, 7, 11
So, answer Is 30.
Sample Input Sample Output
6 960
9 113680
29 73125
26 20520
19 98658
28 73125
26
Problem - 2
Range of queries
You are given an array A with N integers. You are required to answer Q queries of the following
type:
LR
Determine the count of distinct prime numbers that divides all the array values from index L to
R.
Note: Consider 1-based indexing
Input format
The first line contains an integer T denoting the number of test
cases.
• The first line of each test case contains an integer N.
. The second line of each test case contains N space separated
integers denoting A.
The third line of each test case contains an integer Q.
• Next Q lines of each test case contains two space-separated
integers denoting the queries.
Output format
For each test case, print Q integers in separate line denoting the answer to the queries
Constraints
1≤T≤5
1 ≤ N, Q < 105
1 ≤ A[i] ≤ 105
1≤L≤R≤N
Sample input 1 Sample Output 1
1 1
6 1
4 6 3 18 36 54 2
12
36
46
Explanation
Query 1: Only 2 divides all the array elements in range [1,2]
Query 2: Only 3 divides all the array elements in range (3.6]
Query 3: 2, 3 divides all the array elements in range [4.6]
Sample input 2 Sample
Output 2
1 3
9 0
70888 74578 2746 96295 86884 21198 28655 22503 7868
99
68
Sample Input 3 Sample
Output 3
1 3
9 2
75634 28248 27808 75072 58867 37890 95515 26685 68307 2
0
10 1
88 0
99 0
99 4
56 1
68
OA 17:
Problem - 1
Your task is to create a string S consisting of lowercase English alphabets. You are given an
array of size 26
where A[i] denotes the cost of using the ith alphabet (consider 1 based indexing
Find the lexicographically largest string S that can be cleated such that the cost of building the
string is exactly W
Note
The cost of string S is equal to the sum of the cost of the individuals characters present in it
A string P is lexicographically smaller than string Q ≤ P is a prefix Q is not equal to Q or there
exists an i such that Pi < Qi and for all j<i Pj = Qj . For example ‘abc’ is lexicographically smaller
than ‘abcd’ and ‘abd’ is lexicographically smaller than ‘aba’ and is not lexicographically
smaller than ‘ab’ and ‘a’ is not lexicographically smaller than ‘a’
Input Format
The first line contains an integer T denoting the number of test cases
The test time of each test case contains 26 space - separated integers denoting the costs of
characters from ‘a’ to ‘z’
The second line of each test case contains an integer W
Constraints
1<T ≤ 5
1 ≤ A[i] ≤ 10
1 ≤ W ≤ 104
Problem - 2
You are given a weighted undirected tree with N nodes Every edge has a weight associated with
it
You are required to find the value of ΣN-1i=1 ΣNj = i+1 F(i,j) function where F(i,j) denotes the sum of
weight of edges on a simple
node i and j
Since the answer can be very large, print it modulo 109 +7.
Input format
Between
• The first line contains an integer ' denoting number of test case
•The first line of each test case contains an integer N denoting the number of nodes in the tree
• Next N' - 1 lines contain three space separated integers u vs denoting an edge between node
Output format
For each test case, print the value of function modulo 109 + 7 in a new line
Constraints
1≤ T≤5
1 ≤ N ≤ 105
1 ≤ w ≤ 106
Sample Input Sample Output
1 20
121
232
342
Example
ΣN-1 i = 1 ΣN j =i+1 F(i,j) = F(1,2) + F(1,3) + F(1,4) + F(2,3) + F(2,4) + F(3,4)
ΣN-1 i = 1 ΣN j =i+1 F(i,j) = 1 + 3+6 +2 +5 +3 = 20
Sample Input Sample Output
4 146
3 1926
1 3 55 1825
2 3 10 55
4 1 43
5 6 12
2 4 21
276