Uber (Phone) | India | 5 YOE | 95L

Anonymous User
2881
Jan 20, 2025
Jan 21, 2025

Rant Alert: The Coldest Interviewer Ever

So, I had this interview recently, and wow, I’ve never felt so dry in the throat while talking to a screen. Seriously, the interview started 3 minutes late, and the intro took another 3 minutes. That left me with little time to ease into the actual discussion.

The interviewer immediately launched into the problem with zero warm-up. The problem itself was about designing a data structure to represent a terrain of islands in a sea of water, where each island is denoted by a coordinate (x, y). The data structure needed to support two methods:

add(x, y) – Add a piece of land.
is_island(x, y) – Query whether a coordinate is land or water.
I was a bit out of focus, so I initially started thinking of using a matrix, then a list, and after some back and forth, I realized that a simple set could actually do the job. The interviewer, though, was more focused on the drawbacks, time and space complexity of each of these options.

Twenty minutes later, after spending too much time explaining why the earlier methods weren’t optimal, he hit me with a follow-up: "Add another method, island_count() – which returns the number of islands at the time of the query."

Now, I was thinking: “Okay, this feels like a familiar problem—number of islands using DFS/BFS,” but quickly realized that’s not efficient if you’re recalculating the island count every time. So, I suggested using Union-Find to handle it more efficiently and started coding it out.

With 15 minutes remaining, the interviewer pointed out how much time was left, and for some reason, I started to feel a bit panicked—especially since I hadn’t done a dry run of the code. I fumbled through the path compression part and struggled to finish it in time. With about 5 minutes left, the interviewer highlighted some gaps in my logic—like why I wasn’t checking the nei for water or properly defining what id represented (if only he paid attention, I was speaking out loud as I type every character). At that point, he remarked, “This doesn’t look correct,” and seemed in a hurry to wrap up the interview.

Anyway here's the code (I completed it after the interview). I was so close, only if the interviewer was more supportive, wasn't dismissive.

# Regualr UnionFind Datastructure
class UF(object):
    def __init__(self):
        self.id = {}
        self.rank = {}
        self.count = 0
    
    def find(self, t):
        if t not in self.id:
            self.id[t] = t
            self.rank[t] = 1
            self.count += 1

        if self.id[t] != t:
            self.id[t] = self.find(self.id[t])
        return self.id[t]
    
    def union(self, t1, t2):
        # parent of set1
        i1 = self.find(t1)
        i2 = self.find(t2)

        # parent of the merged set
        p = i2

        if i1 != i2:
            r1 = self.rank[i1]
            r2 = self.rank[i2]

            if r1 > r2:
                self.id[i2] = i1
                p = i1
            elif r1 < r2:
                self.id[i1] = i2
            else:
                self.id[i1] = i2
                self.rank[i2] += 1
            self.count -= 1
        return p

class Terrain(object):
    def __init__(self):
        self.grid = set()
        self.groups = UF()
    
    def add(self, x, y):
        t = (x, y)
        self.grid.add(t)
        pid = self.groups.find(t)

        for nei in [(x+1, y), (x, y+1), (x-1, y), (x, y-1)]:
            if self.is_land(nei[0], nei[1]):
                pid = self.groups.union(nei, pid)
    
    def is_land(self, x, y):
        t = (x, y)
        return (t in self.grid)
    
    def island_count(self):
        return self.groups.count
    
# Sample run
if __name__ == "__main__":
    terrain = Terrain()

    grid = [
        [1, 0, 0, 0],
        [1, 0, 1, 1],
        [0, 1, 1, 1],
        [0, 1, 0, 0]
    ]
    
    for r in range(len(grid)):
        for c in range(len(grid[0])):
            if grid[r][c] == 1:
                terrain.add(r, c)
    print(terrain.island_count())
Comments (3)