Label & property indexes·8 min
Build the label index
0 / 1
# grade this stage (from your course directory)
npx theinference test --stage label-index --watchBuild a Map<label, Set<nodeId>> so nodesByLabel('Movie') is instant. One pass over the nodes: for each label on each node, add the node id to that label's set. Unknown labels return an empty array.
Your task
Implement nodesByLabel
Build the label index in the constructor; nodesByLabel(label) returns the sorted node ids for that label. The CLI's by-label Movie command prints them — there are 30 Movie nodes (ids 57–86), and 57 Person nodes.
src/store.ts
// in the constructor:
for (const n of nodes)
for (const label of n.labels) {
let set = this.labelIndex.get(label);
if (!set) this.labelIndex.set(label, (set = new Set()));
set.add(n.id);
}
nodesByLabel(label: string): number[] {
return [...(this.labelIndex.get(label) ?? [])].sort((a, b) => a - b);
}
Process command
your_program.sh by-label {fixture}/fixtures/data/nodes.json {fixture}/fixtures/data/relationships.json Movie
# -> [57,58,...,86] (30 movie ids, sorted)
How it's graded
Process mode, stdout_json_match against the sorted 30-element id array. An unknown label (e.g. Director — a relationship type, not a node label) must return [], not throw.