FAANG questions with solution links:
1.) Design Bounded Blocking Queue ( Leetcode premium) — https://www.youtube.com/watch?v=0B8CXDCtpCU
2.) Web Crawler Multithreaded ( Leetcode premium) — https://www.youtube.com/watch?v=dej0rq-9Xjc
3.) Traffic Light Controlled Intersection ( leetcode premium)—
There is an intersection of two roads. First road is road A where cars travel from North to South in direction 1 and from South to North in direction 2. Second road is road B where cars travel from West to East in direction 3 and from East to West in direction 4.
There is a traffic light located on each road before the intersection. A traffic light can either be green or red.
- Green means cars can cross the intersection in both directions of the road.
- Red means cars in both directions cannot cross the intersection and must wait until the light turns green.
The traffic lights cannot be green on both roads at the same time. That means when the light is green on road A, it is red on road B and when the light is green on road B, it is red on road A.
Initially, the traffic light is green on road A and red on road B. When the light is green on one road, all cars can cross the intersection in both directions until the light becomes green on the other road. No two cars traveling on different roads should cross at the same time.
Design a deadlock-free traffic light controlled system at this intersection.
Implement the function void carArrived(carId, roadId, direction, turnGreen, crossCar)
where:
carId
is the id of the car that arrived.roadId
is the id of the road that the car travels on.direction
is the direction of the car.turnGreen
is a function you can call to turn the traffic light to green on the current road.crossCar
is a function you can call to let the current car cross the intersection.
Your answer is considered correct if it avoids cars deadlock in the intersection. Turning the light green on a road when it was already green is considered a wrong answer.
Example 1:
Input: cars = [1,3,5,2,4], directions = [2,1,2,4,3], arrivalTimes = [10,20,30,40,50]
Output: [
"Car 1 Has Passed Road A In Direction 2", // Traffic light on road A is green, car 1 can cross the intersection.
"Car 3 Has Passed Road A In Direction 1", // Car 3 crosses the intersection as the light is still green.
"Car 5 Has Passed Road A In Direction 2", // Car 5 crosses the intersection as the light is still green.
"Traffic Light On Road B Is Green", // Car 2 requests green light for road B.
"Car 2 Has Passed Road B In Direction 4", // Car 2 crosses as the light is green on road B now.
"Car 4 Has Passed Road B In Direction 3" // Car 4 crosses the intersection as the light is still green.
]
Example 2:
Input: cars = [1,2,3,4,5], directions = [2,4,3,3,1], arrivalTimes = [10,20,30,40,40]
Output: [
"Car 1 Has Passed Road A In Direction 2", // Traffic light on road A is green, car 1 can cross the intersection.
"Traffic Light On Road B Is Green", // Car 2 requests green light for road B.
"Car 2 Has Passed Road B In Direction 4", // Car 2 crosses as the light is green on road B now.
"Car 3 Has Passed Road B In Direction 3", // Car 3 crosses as the light is green on road B now.
"Traffic Light On Road A Is Green", // Car 5 requests green light for road A.
"Car 5 Has Passed Road A In Direction 1", // Car 5 crosses as the light is green on road A now.
"Traffic Light On Road B Is Green", // Car 4 requests green light for road B. Car 4 blocked until car 5 crosses and then traffic light is green on road B.
"Car 4 Has Passed Road B In Direction 3" // Car 4 crosses as the light is green on road B now.
]
Explanation: This is a dead-lock free scenario. Note that the scenario when car 4 crosses before turning light into green on road A and allowing car 5 to pass is also correct and Accepted scenario.
Constraints:
1 <= cars.length <= 20
cars.length = directions.length
cars.length = arrivalTimes.length
- All values of
cars
are unique 1 <= directions[i] <= 4
arrivalTimes
is non-decreasing
Solution: Simple Concept for Multi-threading: -
Use synchroized block to make sure that the execution of the car id should happen only in the way they arrive, and other than that the question is pretty straight forward.
class TrafficLight {
private int currActive;
public TrafficLight() {
currActive = 0;
}
public void carArrived(
int carId, // ID of the car
int roadId, // ID of the road the car travels on. Can be 1 (road A) or 2 (road B)
int direction, // Direction of the car
Runnable turnGreen, // Use turnGreen.run() to turn light to green on current road
Runnable crossCar // Use crossCar.run() to make car cross the intersection
) {
synchronized(this){
if((direction == 1 || direction == 2) && (currActive == 1)){
turnGreen.run();
currActive = ((currActive+1)%2);
}else if((direction == 3 || direction == 4) && (currActive == 0)){
turnGreen.run();
currActive = ((currActive+1)%2);
}
crossCar.run();
}
}
}
4.) Game Play Analysis III( leetcode premium) — https://www.youtube.com/watch?v=Xdfqjknr1Gw
5.) Game Play Analysis IV ( leetcode premium)— https://www.youtube.com/watch?v=qj92XriUohY
6.) Managers with at Least 5 Direct Reports- https://www.youtube.com/watch?v=N0KijybKu_U
7.) Median Employee Salary — https://www.youtube.com/watch?v=CTS_5i0i278
8.) Find Median Given Frequency of Numbers — https://www.youtube.com/watch?v=cmEKMt7peZ4
9.) Big Countries — https://www.youtube.com/watch?v=4urrphbIVc8
10.) Classes More Than 5 Students- https://www.youtube.com/watch?v=bJOXJaV0QB0
11.) Tree Node — youtube.com/watch?v=HWvPJAoy_9I
12.) Exchange Seats- https://www.youtube.com/watch?v=fLvGRA0sdYw
13.) Swap Salary — https://www.youtube.com/watch?v=EIH3xFsl24g
14.) Actors and Directors Who Cooperated At Least Three Times -https://www.youtube.com/watch?v=lq0VOLi0fEk
15.) User Activity for the Past 30 Days I -https://www.youtube.com/watch?v=Pyy-OlcV0-k
16.) User Activity for the Past 30 Days II — https://www.youtube.com/watch?v=mcohcIyaImA
17.) Market Analysis I- https://www.youtube.com/watch?v=8L0da9qxboo
18.) Dynamic Pivoting of a Table -
SQL Schema
Table: Products
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| product_id | int |
| store | varchar |
| price | int |
+-------------+---------+
(product_id, store) is the primary key for this table.
Each row of this table indicates the price of product_id in store.
There will be at most 30 different stores in the table.
price is the price of the product at this store.
Important note: This problem targets those who have a good experience with SQL. If you are a beginner, we recommend that you skip it for now.
Implement the procedure PivotProducts
to reorganize the Products
table so that each row has the id of one product and its price in each store. The price should be null
if the product is not sold in a store. The columns of the table should contain each store and they should be sorted in lexicographical order.
The procedure should return the table after reorganizing it.
Return the result table in any order.
The query result format is in the following example.
Example 1:
Input:
Products table:
+------------+----------+-------+
| product_id | store | price |
+------------+----------+-------+
| 1 | Shop | 110 |
| 1 | LC_Store | 100 |
| 2 | Nozama | 200 |
| 2 | Souq | 190 |
| 3 | Shop | 1000 |
| 3 | Souq | 1900 |
+------------+----------+-------+
Output:
+------------+----------+--------+------+------+
| product_id | LC_Store | Nozama | Shop | Souq |
+------------+----------+--------+------+------+
| 1 | 100 | null | 110 | null |
| 2 | null | 200 | null | 190 |
| 3 | null | null | 1000 | 1900 |
+------------+----------+--------+------+------+
Explanation:
We have 4 stores: Shop, LC_Store, Nozama, and Souq. We first order them lexicographically to be: LC_Store, Nozama, Shop, and Souq.
Now, for product 1, the price in LC_Store is 100 and in Shop is 110. For the other two stores, the product is not sold so we set the price as null.
Similarly, product 2 has a price of 200 in Nozama and 190 in Souq. It is not sold in the other two stores.
For product 3, the price is 1000 in Shop and 1900 in Souq. It is not sold in the other two stores.
Solution-