Google Cracker Sheet
Google Cracker Sheet
- By Sunyul Hossen
Note: [To Use this sheet optimally go and watch the "Cracking the Google coding interview
🔥: The definitive prep guide" video on Debug Buzz Channel.
Link - https://youtu.be/HM8gsynJkQc ]
String getMostActiveUser()
class ChatApplication{
String activeUser;
String mostTransactions;
users.add(receiver);
mostTransactions = users.size();
activeUser = sender;
}
String getMostActiveUser(){
return activeUser;
}
}
eg:
Ans: 2
class EnglishSpanishTranslator {
public:
Eg:
1, 2, 4, 1, 10, 2, 3 4, 5, 7
k=2
answer will be
[1 2 3 4 5 7]
The next random song to be played should satisfy a condition that the song was not
played in the last 'k' turns.
You have to make sure, that at each call, all the eligible (not played during last k turns)
songs have equal probability of being played next.
For example:
given a string s which contains only curly braces and questions was what is the
minimum number of operations to become it valid expression and return corrected
string. There are three operations:
insert { or } in any position
delete { or } in any position. flip any position so { change by } or vice versa.
given N, K and array A length of N and contains non-negative integers. for example
N = 5, K = 2, A = [4, 8, 1, 10, 2]. K is initial work capability which means that you can
work K days, when you work i-th day your capability decreases by 1 and you get
reward A[i], when you skip day your current capability increases by 1. you start at 0
and the question was what is the maximum total reward you can get.
In this example, the answer is 22 because you work 0 and 1 day, after that your
capability will become 0. After that you skipp day 2 and capability becomes 1 and
you work day 3 and you will get 10 reward, in total 4+8+10=22 reward and it's the
maximum you can achieve.
You are given a course prerequisites list in the form {(a,b),(c,d)...}. A semester is a
unit where you can take one or multiple courses. To take a course in a semester, you
must complete all the prerequisite courses of that course. You need to find minimum
number of semesters required to complete all the courses of the curriculum.
(a,b) -> course a is prerequisite to take course b.
Example: Given (1,2) (1,5) (5,2) (2,3) (2,4) (4,6) (5,6) the answer is 5./Which means
you need can complete the curriculum in 5 semesters.
Q) Date: 24th July 2022 -
Constraints :
N,M<=5000
MAX(ai,bi) <=5000
AXA = '/leetcode/config'
BYB = '/%AXA%/interview/corner/file'
LCLS = '/tmp/file/usr/shared/%BYB%'
Input: BYB
Output: '/leetcode/config/interview/corner/file'
Input: LCLS
Output: '/tmp/file/usr/shared/leetcode/config/interview/corner/file'
Given a Doubly Linked list, find a minimum number of API calls ( move()) to sort the given
Linked List. we can only use the move() method to move the node value. We can iterate
over the LinkedList elements as many times as we want but the task is to minimize the
move() calls and sort the LinkedList.
move() - function takes node_val that you want to move and the position of where you
want to place the node at. For example, if you want to pick node_val 5 and place it
between 1 and 6 you can use move(5,1,6). The interviewer mentioned the move function
is flexible and we can use an index as well to move the node. The way the function works
is It would delete the node_val 5 and place it between 1 and 6 like this 2->4->3->1->5-
>6. This is defined as a 1 move() call.
Our task is to minimize the move() call to sort the given LinkedList. For this linkedList 5 ->
2 -> 4 -> 3 -> 1 -> 6, minimum move() calll is 3 to sort the list.
Special Paths
Task
Note: Two paths are different if they contain atleast on different node.
Constraints:
2<=N<=10^5
1<=A[i]<=10^9 ∀i ∈[1,N]
Example:-
N=5
edges =[{1,2},{1,3},{3,4},{3,5}]
A = [2,3,1,2,3]
Output: 2
Explanation:-
Find the count of all pairs that follow the condition : a[i]-a[j]+c <= b[i]-b[j]+d such that i <
j.
Constraints:
● 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
Partition String
You are given a string S of lenght N of digits 0 - 9. You need to partiton strings into K
substrings such that
Determine the number of ways to partitioin the strings which satisfy the above condition
constraints :
1<= m<= n
1<=k<=n
Test cases
n = 9
m= 2
k = 3
s = '232387421'
So there are total 3 ways possible
Write an algorithm that can detect when there is a path from any city of first column to
any city of last column.
Suppose you are working on Google Photos. you are wring the client application.
Request comes to you to upload N photos. you fire the request to server to upload
those N photos to server. Then the server responds back with acknowledgements
that a particular photo is uploaded.
Example. Suppose you are uploading 10 Photos, The server can respond back in any
order, such as 1,2,4,5,3,7,6,10,9,8 . Now at any given point of time we need to check
what is the maximum photo number which has been uploaded continously.
Example.
ack(1),
ack(2),
ack(4)
ack(5)
ack(3)
// initializer
public PhotosClient(int n) {
}
// this method is called each time you receive acknowledgement
forom the server
public void ack(int x) {
}
// this method will be called in between to check what the maximum
photo number has been uploaded successfully
public int getMax() {
}
}
needs to check can we complete all of the process in given start and arrival time with
allocated CPU numbers;
if time is running out they can take one a cpu from their bucket and else they can take
from outside of bucket
[10,20,2]
20 is end time
2 is CPU Number
[[10,20,2], [40,20,3], 5]
You have boxes in magazine and a single empty spot (at the end) like:
1, 4, 2, 5, 3, _
You have a robot which needs to sort boxes. It can only pick up single box, put it in
empty box and then pick another one up. Sort boxes to have either _, 1, 2, 3, 4, 5 or 1, 2,
3, 4, 5, _
What is the fastest way to sort if robot knows immediately where are boxes with specific
numbers, but it takes time for him to move.
Q) Date: 19th May 2022 -
https://leetcode.com/problems/evaluate-reverse-polish-
notation/
Find the Android system from 0.0 -> latest (the interviewer said it is MAX_INTEGER)
different Android installation packages supported by each interval, and then only return
all intervals:
[0.0, 0.0] [1.0,2.0] [3.0, 6.0] [7.0, 7.0] [8.0, latest] -> return these intervals
none v1 v1,v2 v2 none -> do not need to return
input : P1NP2,P2NP3,P3NP1
P1 is North of P2
P2 is North of P3
P3 is North of P1
Input:
[A: [B, D]
B: [A, C]
C: [B, D]
D: [A, C]]
Output: ABCD
Assumption: Graph represents Ring topology find the optimized way to traverse.
Follow up: Check if Graph represent ring topology check for all corner cases.
[35,32,43,41,39] 31 to 45
[etc...]
Create a void function which generate the game and return 2D array which contains
game
For example,if we have a string "abcd" and want to find the char at index 3 it would
obviously be 'd'. However, if we want to find the char at index 7 of "abcd" we would
transform "abcd" to "abcddabc" and the char would be 'c'. If we want to find the
char at index 12 of "abcd" we would transform "abcd" to "abcddabc" and then again
transform to "abcddabccabcddab" and the char would be 'd.
e.g
Rule Conditions Value
R1 C1,C2,C3 40
R2 BCD1,BC5 40
R3 BCD1,BC5 80
Here, R2 and R3 conflict.
e.g
R1 C1,C2,C3 40
R2 BCD1,BC5 50
R3 BCD1,- 50
R6 -,-,C3 40
No conflicts.
indexed).
We are interested in a special version which return a character even if i >= s.length()
The string should transform like below. Basically do the following => ABC + append the
same string again, just move the last char to the front
Follow-up: given the printed string, make the tree and return the root.
Example:
1
/ | \
2 3 4
| / \ |
5 6 7 8
|
9
print the output in this format (append a dash "-" to each node value and
keep adding dashes for each level)
-1
--2
---5
--3
---6
---7
----9
--4
---8
Question
Everyone is well aware of Google Maps. We are Installing a new feature into Google
Maps. The feature would immediately output the minimum speed required to make
a trip.
You are given a sequence of distances between places in the trip. You are given a
threshold time. You need to make a trip within this time. Output the minimum
speed.
Example :-
INPUT -
Distance - [5,6,3,9,4]
Threshold Time - 8
OUTPUT -
Explanation -
For the Distance - [5,6,3,9,4] with a minimum speed 5, I can cover 5 units distance at
0th index in 1 unit time. Can cover 6 units distance at 1st index in 2 unit time. Can
cover 3 units distance at 2nd index in 1 unit time. Can cover 9 units distance at 3rd
index in 2 unit time. Can cover 4 units distance at 4th index in 1 unit time.
Total Time Taken - 7 Units which is less than the Threshold of 8 units. So 5 unit
speed is the minimum speed possible to make the entire trip.
Question
We are given a list of Employees with their ID, manager ID, and Name. The question
ask us to print the names of all the employees in a hierarchy.
Note: [The Ultimate manager is the one with the highest ID Value and whose ID is
same as the Manager ID]
Output:-
// ---Bob
// ------Emp3
// ---------Emp4
// ------------Emp5
// ---------Emp6
// ---Emp7
x=0 with probability of 1. We can go right, left, or stay where we are at each integer
time step with probabilities p1, p2, 1-p1-p2. time increments as dt = 1. Every time
you move right, left, or stay where you are (every action), time increments 1 unit.
[NY, 9]
[SF, 1.5]
[Atlanta, 3]
[NY, 5]
[Boston, 1]
[SF, 3]
Example:
Rob - [A, B, C]
B - [A, C, D, F, Rob]
A - [B, C, D, E, Rob]
C - [A, B, J, Rob]
D - [B, A, F]
G - [H, J]
J - [H, G, K, M]