Six Degrees, Give or Take — JavaScript Bug Hunt

Inspired by the "2nd-degree connection" labels of professional networks — which must be the shortest path between two people.

  • Language: JavaScript
  • Layer: Frontend
  • Difficulty: Hard
  • Concepts: Graphs, BFS
  • Modelled on: LinkedIn
  • Visible tests: direct connections are first degree; second degree via a mutual; unreachable members are -1, self is 0
  • Reward: 50 XP for a complete fix

Briefing

Inspired by the "2nd-degree connection" labels of professional networks — which must be the shortest path between two people. Depth-first exploration returns the first path it stumbles into, and profiles show "3rd" for direct connections.

degrees.js computes connection degree. It needs breadth-first, not depth-first.

Bug report

BUG-DEGREES · Priority: High · Reported by: profile team

degree(network, from, to):

  • the SHORTEST connection distance (1 = direct)
  • unreachable -> -1, self -> 0

Observed: alice and dave are directly connected, but because the traversal dives down alice → bob → carol → dave first, the label says 3rd.

Logs

[profile] degree(alice, dave) = 3 (they are 1st-degree!)

The code as shipped

src/graph/degrees.js (editable)

// Connection degree between two members.
exports.degree = function (network, from, to) {
  var visited = {};

  function dfs(node, depth) {
    if (node === to) return depth;
    visited[node] = true;
    var neighbors = network[node] || [];
    for (var i = 0; i < neighbors.length; i++) {
      if (!visited[neighbors[i]]) {
        var found = dfs(neighbors[i], depth + 1);
        if (found !== -1) return found;
      }
    }
    return -1;
  }

  return dfs(from, 0);
};

Open the hunt to edit the files, run the visible tests and submit against the hidden ones. More JavaScript bug hunts.