Uber Cracker Sheet
Uber Cracker Sheet
- By Sunyul Hossen
🔥:
Note: [To Use this sheet optimally go and watch the "Cracking the Uber coding interview
The definitive prep guide" video on Debug Buzz Channel.
Link - https://youtu.be/CYKnE1FiVzU ]
Given a solution as an array of strings crypt, count the number of its valid solutions.
The solution is valid if each letter represents a different digit, and the leading digit of any
multi-digit number is not zero.
crypt has the following structure [word1, word2, word2], which stands for the word1 +
word2 = word3 solution
EXAMPLE
Our Old Man is really old, and has no desire for money. His world is a NN X MM grid and
he lives in his cave at (1,1)(1,1). He want to meet his friend GodMan who lives in a
mountain at (N,M)(N,M).
There are X locations in this grid where we have a Coin.
But Old Man is on path to Nirvana and now doesn't want any money in his life and just
want to meet his friend.
Find the no. of Paths from (1,1)(1,1) to (N,M)(N,M) in which old man can reach his friend
while avoiding any coins in his path thus attaining nirvana. Since there can be too many
paths, so print it Modulo 10^{9} + 710
9
+7.
Additionally for any two locations with coins, lets say (a,b) and (p,q) we have either,
(aa \lt< pp and b\gt> qq) or (aa \gt> pp and bb \lt< qq)
Constraints
4 Paths are
2 Paths are
[input] integer n
[input] integer m
coins is a vector of vector int with each vector int containing exactly 2 integers indicating
the location of coin.
Additionally each coin lies within the grid. (see input in sample test cases for more
understanding)
[output] integer
Q) August 3rd, 2022
Output:- 3
Write a function that returns whether or not it is possible to get to the last square from the
starting square in any number of turns.
Examples:
+------------------+
| +-----+ |
vv||
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
^^||
+-----|----------+ |
+--------------+
With a 4 sided die, starting at square 0 with a board ending at square 20 (as pictured
above)
No matter what you roll, it's not possible to get past the teleporters from 10-13.
finishable(teleporters1, 4, 0, 20) => False
If an additional teleporter was added from square 2 to square 15, this would be possible to
finish.
1≤ ∣a∣ ≤10^5
1≤ ∣b∣ ≤10^5
Input
contest
son
Output
10
On the other hand, b is not a subsequence of the string contestco (the first 9 characters in
a'), so i=9 does not satisfy the condition.
Similarly, any integer less than 9 does not satisfy the condition, either. Thus, the minimum
integer i satisfying the condition is 10.
Input
contest
sun
Output
-1
Input
He grows a crop which has a water-withstanding capacity c in patch i (if that patch already
had a crop, it is removed to grow this new one)
He starts the sprinkler for the patches in the range from l to r - 1 which sprinkles water at
an intensity w. Due to this, all the crops in the patches l to r - 1 whose water-withstanding
capacity is not more than w (c ≤ w) get damaged and John needs to remove them.
Note: The water-withstanding capacity c of a crop having (c > w) does not decrease after a
sprinkling event.
You have to find out how many crops are damaged after each sprinkling event.
Input
n and k, the number of patches and the number of events respectively (1 ≤ n, k ≤ 200000).
A matrix events of size k to describe the events. Each event is a list and is described as
shown below:
(0 ≤ i < n, 1 ≤ c ≤ 1e9)
(0 ≤ l < r ≤ n, 0 ≤ w≤ 1e9)
Output
A list containing the number of crops damaged for each event of type 2 (sprinkling event).
Example
Input
n: 5
k: 9
events:
[[1,0,3],
[1,2,5],
[2,0,4,3],
[1,1,4],
[1,2,7],
[2,1,3,6],
[1,3,6],
[1,4,5],
[2,0,5,6]]
Output
[1, 1, 2]
A colourful transition is a close continuos line through which there should be change of
colour. Given co-ordinate of two hoops find the number of circular colourful transitions.
Input:
x,y,r,R
where x,y is co-ordinate of center of the hoop. r and R are the radii of the smaller and
larger concentric circles of the hoop respectively.
Output:
Constraints:
-100<=x,y<=100, 1<=r<R<=100
Sample Input
60 60 45 55
80 60 15 25
Sample Output
Given an arr of length n, find its number of subarrays which are beautiful.
n<=10^5
We want to divide these N cities into X disjoint groups G1, G2....Gx such that any one of
the following conditions meet for any bridge that connects two cities A and B:
Constraints
Input Format
N line follows, one for each city, ith line contains N characters , where the jth character is
1 if there is a bridge between city i and j, else the character is 0
Sample Input 1
0111
1011
1101
1110
Sample Output 1
-1
Explanation
It is not possible to divide the cities into different groups by following the given
constraints
Sample Input 2
01
10
Sample Output 2
2
Explanation
City 1 can be in 1st group and city 2 can be in 2nd group, hence, max value of x is 2
Sample Input 3
011
100
100
Sample output 3
Explanation
City 2 can be in 1st group, city 1 can be in 2nd group and city 3 can be in 3nd group,
hence, max value of x is 3
[input] integer n
N line follows, one for each city, ith line contains N characters , where the jth character is
1 if there is a bridge between city i and j, else the character is 0
[output] integer
Value of X
There is a fee E_i to host the show on each i_th road, and the total cost of roadshow is
defined as the bitwise OR of all road fees in the itinerary plan. The council has specifically
asked that there be no cycles in the final itinerary plan.
Find the minimum possible cost of roadshow in the country.
Output : The minimum possible cost of roadshow will be by using the roads between
[[2,3], [1,3]]. And the bitwise OR is 2.
If we used other combinations such as [[1,2],[2,3]] or [[1,2], [1,3]], the bitwise OR would be
3.
If we used all edges, the bitwise OR would be 3, and it will have cycles.
N = number of cities
M = number of roads
This is a Mx3 matrix. For each 0 <= i < E, roadFees[i] = [u_i, v_i ,E_i]
where u_i, v_i are the cities that the road connects and E_i its road fee
[output] integer
A integer set T of size L is called good if all integers in T are distinct and lie in range [1,N][1,N].
Let S be the set of all such possible sets T of given length LL.
Since the problem is too easy for you, you will have to solve this for Q such query.
i^{th}i
Constraints:
3
6
Sample Input
ks: [2, 1, 2, 3]
ls: [2, 1, 2, 3]
ns: [3, 1, 2, 3]
Sample Output
[8, 1, 2, 3]
K=L=2,N=3K=L=2,N=3
K=L=N=1K=L=N=1
K=L=N=2K=L=N=2
K=L=N=3K=L=N=3
2nd smallest value in all of these sets resp. is 2,3,32,3,3, sum of these is 88.
[input] array.integer ks
[input] array.integer ls
[input] array.integer ns
[output] array.integer
FUNCTION ->
[O E E E E]
[E O X E E]
[X E X X E]
[E E O X E]
[X E X E E]
Query:
[2, 2, 4, 1]
Output: Robot at (1,1). List<int[]> is return type. Here, output will be [ {1,1} ].
Explanation: Robot at (1,1) has distance of 2 on left, 2 on top, 4 on bottom and 1 on right.
How is distance calculated? The robot at (1,1) has distance of 2 on left to border. E and
then the border, so distance of 2. Similarly, on the top E and then a border so distance of
2. On bottom, 3 E + border (out of bound) = 4. On right, obstacle X at distance of 1.
So, the query is [2,2,4,1] representing distance to obstacle on left, top, bottom, right. For
this query, robot at index (1,1) satisfies the query so return that robot. There might be
many robots in grid that satisfy the query or even none robot that satisfies this query
There can even be multiple robots satisfying this query. Return all the robots that satisfy
the query.
Consider this as a read heavy system. So, there will be lot of queries. Focus should be on
pre-computing efficiently.
Help the hotel manager in correcting these mistakes. If there is an overlap in the multiple
intervals listed, merge those intervals. Output a list of intervals where there is no overlap.
Examples
Input1: [(Mon 9:00 -> Mon 17:00), (Tue 9:00 -> Tue 17:00)]
Output1: [(Mon 9:00 -> Mon 17:00), (Tue 9:00 -> Tue 17:00)]
Input 2: [(Fri 17:00 -> Sat 2:00), (Sat 1:00 -> Sat 15:00) ]
Output 2 : [(Fri 17:00 -> Sat 15:00) ]
Input 3: [(Mon 9:00 -> Mon 12:00), (Mon 12:00 -> Mon 17:00)]
Output 3: [(Mon 9:00 -> Mon 17:00)]
Each node also has a value associated with it, given as an array val of size N.
For each node V, you have to calculate the number of nodes in the subtree of V whose
value is co-prime with the value of V.
You need to return the sum of this value for all nodes in the tree as an answer.
Constraints
Sample Input:
n: 5
par: [1, 1, 3, 3]
val: [1,2,3,4,5]
Sample Output:
Sample Explanation:
par array is [1,1,3,3]. This means parent of node 2 and node 3 is 1, and parent of node 4
and node 5 is 3.
val array is [1,2,3,4,5]. This means values of nodes 1,2,3,4 and 5 are 1,2,3,4 and 5
respectively.
1 repesents robot
2 repesents blocker
There can be many robots in the matrix. The task was to return all the robots as per the
query below:
[2, 4, 3, 5] which means return all robots which have blockers at distance:
EX:
arr = [1, 4, 3, 2]
result = 4 (Add 4 to the first item)
arr = [2, 3, 2, 3]
result = 4 (Add 2 to the last 2 items)
For a string, find the smallest possible number of steps to make given string S, a special string.
Example 1 :
Input : S1 = "aaxa"
Output : 1
Example 2 :
Input S1 = "cathatratmat"
Output : 2
Example 3 :
Input S1 : "aabcdeccdecc"
Output : 4
Constraint :
You task is to check whether it is possible to apply the swap operation at most once, so
that the elements of the resulting array are strictly increasing.
t1 John Seattle
t2 Doe Singapore
t3 John Delhi
t4 Doe Beijing
t2 John Beijing
t1 Doe Delhi
t4 John Singapore
t3 Doe Delhi
These are log of user and there location in a particular timestamp ( Long Format ) Order is non
sorted.
Create Sequential combination of cities for 3 consecutive timestamps (t1, t2, t3). (t2,t3,t4) etc
with count of users appearing in those cities accordingly.
and so on.
No limit in number of users or cities or timestamp. In Memory solution can be discussed, how
would you scale it from millions of logs.
First choose an integer K and then perform the following operation any number of times :
● Choose any substring of this string such that length of substring is greater than or equal
to K and change all 0s to 1 and all 1s to 0 in that substring i.e. for a substring [L,R]
(R-L+1>=K) in s, for each i such that L<=i<=R, if si is 0 make it 1 and if it is 1 make it 0.
Find the maximum integer K less than or equal to n that we can choose such that it is
possible to convert the string to all 0s using above method.
Constraints
1<=n<=100000
Examples
Input : 101
Output : 2
Explanation : If we choose K = 2, we can convert the string to all 0s by following way
● choose substring [0,1] = "10", then s becomes 011
● now choose substring [1,2] = "11", then s becomes 000
It is the maximum K as we can not convert the sting to all 0s by choosing K = 3.
Input : 10000
Output : 4
Explanation : We can choose K = 4 and convert the following way
● Choose substring [0,4] = "10000", the s becomes 01111
● Choose substring [1,4] = "1111", then s becomes 00000
Input/Output
● [execution time limit] 4 seconds (py3)
● [input] string s
given binary string
● [output] integer
Output the maximum K such that it is possible to convert s to all 0s by the operation.
Note
Strings will have max length 100
p: ab
q: ba
r: aba
output: 2
from p, a, from q, ba
[input] string p
[input] string q
[input] string r
[output] integer
// Returns a string
cout << "This prints to the console when you Run Tests" << endl;
}
Q) May 7th, 2022
You and your best friend leave messages for one another in your favorite places around town.
But you don't want any strangers to read them, so you hide them within strings. To decode a
message in a string you need to find a string subsequence of length 5 which is a palindrome -
this subsequence is the code itself.
Given a string message, return the number of such subsequences within it.
Example
solution(message) = 2.
solution(message) = 4.
You can obtain "abcba" 4 times with different a and c from the given string.
Input/Output
Guaranteed constraints:
1 ≤ message.length ≤ 700.
[output] integer
The number of subsequence palindromes with exactly 5 letters in the given string. It's
guaranteed that for the given test cases the answer always fits signed 32-bit integer type.
// Returns a string
string helloWorld(string name) {
cout << "This prints to the console when you Run Tests" << endl;
Each node also has a value associated with it, given as an array val of size N.
For each node V, you have to calculate the number of nodes in the subtree of V whose
value is co-prime with the value of V.
You need to return the sum of this value for all nodes in the tree as an answer.
Constraints
Sample Input:
n: 5
par: [1, 1, 3, 3]
val: [1,2,3,4,5]
Sample Output:
Sample Explanation:
par array is [1,1,3,3]. This means parent of node 2 and node 3 is 1, and parent of node 4
and node 5 is 3.
val array is [1,2,3,4,5]. This means values of nodes 1,2,3,4 and 5 are 1,2,3,4 and 5
respectively.
Sample Tree
For node 1, the nodes in its subtree whose values are co-prime with value of 1, i.e. 1 are:
2,3,4 and 5. Count: 4
For nodes 2,4 and 5 there are no such nodes as they have no subtree.
For node 3, the nodes in its subtree whose values are co-prime with value of 3 i.e. 3 are: 4
and 5. Count: 2.
So final answer = 4 + 2 = 6.
Input Generation
You are not directly given the arrays p and val. Instead you are given four integers N,
seed, l, r and s for randomly generating the arrays p and val. Use the following stub to
begin with:
int p[100001], val[100001]; // global arrays which will be randomly generated by below
function
srand(seed);
for(int i=1;i<=N-1;i++) {
p[i] = max(1, i-S+1) + (rand() % (i+2 - max(1, i-S+1))); // random node
in [i-S+1, i+1]
}
for(int i=1;i<=N;i++) {
val[i] = L + rand() % (R-L+1); // random integer between L and R
}
generateTree(n, seed, l, r, s); // Call the random generator to generate p and val arrays first
// Write your code from here
[input] integer n
[input] integer l
[input] integer r
[input] integer s
[output] integer64
The sum of the number of co-prime values in the subtree, for each node.
First List
Explanation:
What this means is that at timestamp 1 the value is 3, and then at timestamp 3 the value is 1 and
so on.
Note: Missing/Undefined value on any timestamp is extrapolated backwards from the next
timestamp. For example, value at timestamp 2 based on the example above would be 1.
Second List
Write a function to aggregate these two time series data into one
Answer
[(1, 6), (2, 4), (3, 4), (5,6), (6, 7), (9, 3), (10,1)]
Example:
Input:
Output:
It is guaranteed that there is a path through the object connected by it's sides and not through it's
corners.
Follow up - What if multiple routes are available and output the longest route possible.
[["JFK","SFO"],["ATL","CDG"],["SFO","ATL"],["CDG","MAA"],["MAA","ORD"],["ORD","DFW"],["CD
G","DXB"]]
Valid routes -
You know the weather forecast — the string 𝑠 of length 𝑛, consisting only
of letters U, D, L and R. The letter corresponds to a direction of wind.
Moreover, the forecast is periodic, e.g. the first day wind blows to the
side 𝑠1, the second day — 𝑠2, the 𝑛-th day — 𝑠𝑛 and (𝑛+1)-th day — 𝑠1 again
and so on.
if wind blows the direction D, then the ship moves from (𝑥,𝑦) to (𝑥,𝑦−1);
if wind blows the direction L, then the ship moves from (𝑥,𝑦) to (𝑥−1,𝑦);
if wind blows the direction R, then the ship moves from (𝑥,𝑦) to (𝑥+1,𝑦).
The ship can also either go one of the four directions or stay in place
each day. If it goes then it's exactly 1 unit of distance. Transpositions
of the ship and the wind add up. If the ship stays in place, then only the
direction of wind counts. For example, if wind blows the direction U and
the ship moves the direction L, then from point (𝑥,𝑦) it will move to the
point (𝑥−1,𝑦+1), and if it goes the direction U, then it will move to the
point (𝑥,𝑦+2).
You task is to determine the minimal number of days required for the ship
to reach the point (𝑥2,𝑦2).
What is the maximum number of people that can visit the haunted house at one time so
that no constraint is violated?
Sample Input:
// L R
1 2
1 4
0 3
0 1
3 4
0 2
Sample Output
3
https://leetcode.com/problems/bus-routes/
Ex Input:
Stock:
write the methods for lowest(), highest() stock until now.. the stream will continue..
whenever we call this function it should return the lowest value of the stock till now, and if
we call the highest value function, it should return the highest value of stock till now.
If X does a campaign in town T, both M1 and M2 number of people in this town become his
supporters.
Your task is to calculate the least number of towns in which X has to campaign to win the
election.
Note: It is always possible for X to win the election.
Constraints:
Test Case
22
22
Answer: 1
Explanation: X can campaign in either city and gain 4 votes, Y can thus only get a
maximum of 2 votes
22
13
51
22
Answer: 1