Design a Tag Management System
🏷️ What is a tag management system? It is the part of an issue tracker that lets people stick short labels like
bug,p1orgood first issueon issues and tickets. Later, people find work by label: "show me everybugthat is alsop1", or "anything taggediosorandroid". A side panel lists the most used tags.
GitHub calls them labels on issues and pull requests, Jira calls them labels on tickets, and blogs and Q&A sites call them tags on posts. The design is the same in all of them: a many-to-many link between items and short names, plus fast search over it. We design the GitHub and Jira case and keep the item side generic, so the same code tags anything with an id.
This is a good easy problem because the classes are few and the interesting part is one data structure choice. Interviewers use it to see whether you notice that "find items by tag" needs its own index.
Requirements
"Design a system to tag issues, like labels on GitHub or Jira. Users can add and remove tags and search issues by tag."
Short, so we ask. Each answer below settles a rule we will code against.
Clarifying Questions
You: What can be tagged? Only issues, or also pull requests, tickets, documents?
Interviewer: Start with issues, but other teams want the same feature for their items. Treat an item as just an id like "BUG-1" or "PROJ-42".
You: Are Bug, bug and bug the same tag?
Interviewer: Yes. People type tags by hand and we keep getting duplicates. Trim spaces at the ends and ignore case. Spaces inside a tag are fine, like good first issue.
You: For a search with several tags, does an item need all of them or any of them?
Interviewer: Both. "All" for narrowing down (bug and p1), "any" for widening (ios or android).
You: In what order do search results come back?
Interviewer: Sorted by item id, so results are stable. Tags on an item are listed alphabetically too.
You: Do tags need to be created up front, with colors and descriptions?
Interviewer: Not for now. A tag exists while at least one item has it. We can talk about metadata later.
You: Is there anything like "most used tags"?
Interviewer: Yes, the sidebar shows the top k tags by how many items have them. Ties alphabetically.
The second answer is worth repeating to the interviewer, because it decides where the rule lives. If every method trims and lowercases on its own, one of them will forget. We want a single place that turns user input into a tag name.
💡 Interview tip: Ask how search results are ordered. "Sorted by id" sounds like a detail, but it decides whether your output is deterministic, and it is how you and your interviewer (and the practice tests) agree on an answer.
Final Requirements
- An item is any string id. Items do not need to be registered first.
- Users can add a tag to an item and remove a tag from an item. Adding a tag the item already has, or an empty tag, fails. Removing a tag the item does not have fails.
- Every tag argument is normalized: spaces at the ends are removed and letters become lowercase. Normalized names are what we store and compare.
- Users can list an item's tags, sorted.
- Users can search for items that have all of a list of tags, or any of them. Results are sorted by item id.
- Users can ask for the top
ktags by number of items, ties by name. - A tag that no item has any more disappears (it no longer counts in the top list).
Out of scope
- Tag metadata: colors, descriptions, who created a tag (we come back to it in Extensibility).
- Permissions: who may tag what.
- Full-text search on item titles and bodies.
- Persistence and several servers (also in Extensibility).