AlgoViz

See it run

Follow stored links, see a walk spread, write without duplicates, and commit with a majority.

Operations

Anchor and expand

Find Ana's grandchildren in the family graph.

Step 1 of 5

The graph

parentparentparentmarriedmarriedAnaBenCyDotEve

5 nodes, 5 relationships

Five people, three parent links and two marriages. The arrow is the stored direction.

Step 1: The graph

def grandchildren(index, key):    start = index[key]                 # the anchor    out = []    for child in start.out["PARENT_OF"]:        for g in child.out["PARENT_OF"]:            out.append(g)    return out
Work
1 lookup, 2 hops
Watch out for
Missing index
Scan first
Reads every person

The other branches of the graph are never read.

In the real world

The same neo4j doing real jobs. Pick one and step through it.

A1 to A4 are accounts. Ph is a phone and Ad is an address.

Step 1 of 5

Four accounts

A1PhphoneA2AdaddressA3A4

4 accounts

In their own rows nothing is strange.

Step 1: Four accounts

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • No uniqueness constraint on the key

    critical

    Every MERGE scans the label, and two requests at once can create two nodes.

    Wrong
    MERGE (p:Person {id: $id})   // no constraint
    Right
    CREATE CONSTRAINT person_id IF NOT EXISTSFOR (p:Person) REQUIRE p.id IS UNIQUE
  • An unbounded path

    critical

    Work grows with degree to the power of hops until memory or patience ends.

    Wrong
    MATCH (a:Person {id: $id})-[*]-(b)
    Right
    MATCH (a:Person {id: $id})-[:FOLLOWS]->{1,3}(b:Person)
  • Values pasted into the query

    major

    Each text is a new plan, and input can become code.

    Wrong
    run("MATCH (p {id: '" + user_input + "'})")
    Right
    run("MATCH (p:Person {id: $id}) RETURN p", id=user_input)
  • Read, compute, write back

    major

    Read committed lets another transaction change the value in between.

    Wrong
    n = read("RETURN p.likes")write("SET p.likes = $n", n + 1)
    Right
    SET p.likes = p.likes + 1

Interview tips

  • How to explain it

    • Links are stored as data.
    • An index finds the first node, then we walk.
    • Cost is the neighbourhood touched.
  • Say three things

    • The type.
    • The direction.
    • The depth limit.
  • Reach for it when

    • The path is the answer.
    • The number of steps is unknown.
    • Shared things join people.
  • Choose something else when

    • You add up everything.
    • One join is enough.
    • Writes exceed one leader.