Skip to curriculum
Jump to the editor

4.14. Index topics by tag and look them up

Medium
Topics

This question is in

4. Array utilities
20–35 min

A learning site has thousands of topics, each with a few tags such as "css", "layout" and "performance". A learner picks tags from a filter bar, and the page must show the topics that have all of them, instantly, on every click. Scanning every topic for every click works for fifty topics and becomes slow for fifty thousand. The usual answer is an index, built once: for each tag, the list of topics that have it. A lookup then only reads the lists for the tags it needs.

Write buildTagIndex(topics) and findByTags(index, query) in tagIndex.js.

Signature

JAVASCRIPT
function buildTagIndex(topics)      // returns a Map: tag → array of topics
function findByTags(index, query)   // returns a new array of topics
  • Each topic is an object such as { id: "grid", title: "CSS Grid", tags: ["CSS", "layout"] }. tags may be missing or empty.
  • A tag is normalized by trimming spaces and lower-casing it, so " CSS " and "css" are the same tag. A tag that is empty after trimming is ignored.
  • query is one tag (a string) or an array of tags.

Requirements

buildTagIndex:

  1. It returns a Map whose keys are normalized tags and whose values are arrays of the topics with that tag.
  2. Inside each array, topics are in input order, and a topic appears only once even if its tags repeat (["CSS", "css "]).
  3. Blank tags are ignored; a topic with no tags, or an empty list, is in no array.
  4. The arrays hold the topic objects themselves. An empty list of topics gives an empty Map. The topics are not changed.

findByTags:

  1. It returns the topics that have every tag in the query, in input order (whatever the order of the query's tags).
  2. The query is normalized the same way, can be a string or an array, and may repeat a tag.
  3. An unknown tag gives []. An empty query, or one with only blank tags, gives [].
  4. The result is a new array: changing it does not change the index.
  5. It uses only the index: topics added to the original list after the index was built are not found.
  6. It is fast: when one tag has 20,000 topics and another has 2, a thousand lookups for both take a small fraction of a second. Building the index for 20,000 topics is fast too.

Examples

JAVASCRIPT
const topics = [
  { id: "grid", tags: ["CSS", "layout"] },
  { id: "flex", tags: ["css", "Layout "] },
  { id: "lcp", tags: ["performance", "CSS"] },
  { id: "notes" },
];
const index = buildTagIndex(topics);
index.get("css").map((t) => t.id);     // ["grid", "flex", "lcp"]
index.get("layout").map((t) => t.id);  // ["grid", "flex"]
JAVASCRIPT
findByTags(index, ["Layout", "css"]).map((t) => t.id);   // ["grid", "flex"]
findByTags(index, "PERFORMANCE").map((t) => t.id);       // ["lcp"]
findByTags(index, ["css", "video"]);                     // []: no topic has "video"
findByTags(index, []);                                   // []

Environment

  • Plain JavaScript in a browser page; the tests call the globals buildTagIndex and findByTags from tagIndex.js.
  • Tags are strings. Topic objects are not changed after the index is built.
  • Any built-in may be used.

Hints

  1. A topic with all the query's tags is in every one of their lists. Which list is the cheapest to start from?
  2. To check "does this topic have that tag?", do you need to search a list of 20,000 topics, or is there something smaller to look at?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…