Google L4 | Phone Screen
An SMS can be received/transmitted by a service tower from/to a
phone or another service tower located in its vicinity (left, right, up or down).
For example:
a message can be sent from s to d:
sTTT
T
TTTda message cannot be sent from s to d:
TTTd
T
sT TThere is a field of size rows x cols and it is initially empty.
The connectivity infrastructure team (CIT) is building service towers
so that people can communicate without crossing the field.
They build these towers at a rate of 1 per day.
cols
+----+
| |
| |
rows | |
| |
| |
+----+We are unaware of how the CIT is building these towers
but we must let them know when the left side is connected to the right side
so that they stop building towers that are unnecessary. We should also
return the number of days spent by the CIT (or number of towers built, which is the same value).
To simulate the passage of a day and get a new tower,
use API nextTower() which returns an integer point object (r, c)
where 0 <= r < rows and 0 <= c < cols.
To inform the CIT to stop building towers, use API stop().
Example:
Day 0: nextTower() returns (2, 1)
Day 1: nextTower() returns (0, 2)
Day 2: nextTower() returns (4, 1)
Day 3: nextTower() returns (3, 2)
...
The numbers on the grid show what day a tower was built.
0123
+----+
0 |.81.|
1 |.7..|
2 |.096|
3 |..3.|
4 |425.|
+----+With these service towers, the left side is connected to the right side (left->4->2->5->3->9->6->right).
In this case, we should call API stop() on day 9, then return 10.
I was able to come up with bfs solution straightaway but interviewer(from China) asked for optimal solution i have struggled to find optimal solution and discussed about farthest node that can be reached from left most column but that was not optimal and also we ran out of time. I have checked after interview it was related to dynamic connectivity problem which can be solved using Union-Find.
Is dynamic connectivity concept common atleast in CP? I spent almost month completely on graph problems yet i was not able to come up with optimal solution I felt too dumb post interview.


