Legacy Archive (2019): Originally written in Chinese on doubixie.com and translated into English for this edition. All archive posts · Home

ByteDance 2020 Interview Problem: "Douyou" Bottles

Original Chinese title: 字节跳动2020面试题 - 豆油瓶题

ByteDance “Douyou” Bottles Problem

  • While slacking off from studying and browsing V2EX, I saw someone share a 2020 coding interview problem. Thought it was pretty fun, so I’m jotting it down here.

Problem Statement and Solution

Problem Description:
Douyin has hundreds of millions of daily users. If users A and B interact at least 3 times,
we consider A and B to be "Douyou" (Douyin friends).
If A and B are "Douyou", and B and C are "Douyou", then A and C are also "Douyou" with each other.
We define a "Douyou bottle" as a group formed by direct and indirect friends.

Given an N×N matrix M representing interaction counts among all users on Douyin:
if M[i][j] = 5, user i and user j have interacted 5 times;
0 means no interaction;
and for i = j (the same user),
the interaction count is set to 0. Please compute and output the total number of "Douyou bottles" found on Douyin.

Input Description:
Line 1: N, the number of rows (and columns) of the 2D array.
The next N lines each contain N space-separated integers.

Output Description:
Output the number of "Douyou bottles" found, k.

Personal Notes

  • The problem setup doesn’t provide a way to generate test data,

  • so I wrote one with NumPy:

  • build a random matrix, take the upper triangle, and add its transpose minus twice the diagonal to get the symmetric test data the problem expects.

    def samples(num):
        net = np.random.randint(0,4,num**2).reshape(num,num)
        net = np.triu(net)
        net+=net.T - np.diag(net.diagonal())*2
        return net
  • The solution idea is pretty standard:

  • keep a list to track bottle assignments, loop from the first unvisited user, mark everyone reachable via BFS, and increment the counter by 1 once a traversal finishes.

  • I tweaked it slightly to also print which users belong to which bottle at the end.

    def solution(num,net):
        vis = [0]*num
        res = 1
        while min(vis) == 0:
            t = [i for i in range(num) if vis[i] == 0]
            vis[t[0]] = res
            q = []
            q.append(t[0])
            while q:
                h = q.pop(0)
                for j in range(num):
                    if net[h][j] >=3 and vis[j] == 0:
                        q.append(j)
                        vis[j] = res
            res += 1
        print(vis)
        return res-1

An Interesting Observation

  • The problem states nn<200, and I noticed that once nn=200, the number of bottles is basically guaranteed to be 1.
  • At first I thought my code had a bug, until I plotted a scatter plot for ∑_3200n\sum\_3^{200}{n} and realized my program was actually right.

1

  • Maybe it’s just another manifestation of the six degrees of separation theory. Once a person interacts more than 3 times with a certain number of people, they’ll find shared interests and end up in the same bottle. I’ve known the theory for a long time, so seeing a quantitative metric like this—and the result—is pretty fun.
  • Neat.