Hone logo
Hone
Problems

Implementing a Tagged Values System in JavaScript

This challenge asks you to design and implement a system in JavaScript that allows associating arbitrary tags with values. This is useful for scenarios like metadata management, filtering data based on specific attributes, or creating flexible configuration systems where values are identified by multiple criteria. The goal is to create a data structure that efficiently stores and retrieves values based on their associated tags.

Problem Description

You need to implement a TaggedValues class in JavaScript. This class should allow you to:

  1. Store Values with Tags: Add a value to the system, associating it with one or more tags. Tags should be strings. A single value can have multiple tags.
  2. Retrieve Values by Tags: Retrieve all values associated with a specific tag (or set of tags).
  3. Remove Values: Remove a value from the system, optionally specifying the tags associated with it. If no tags are specified, remove the value from all tag associations.
  4. Check for Value Existence: Determine if a value exists in the system, optionally with specific tags.

Key Requirements:

  • The TaggedValues class should be able to handle a large number of values and tags efficiently.
  • Tag lookups should be reasonably fast.
  • The system should be flexible enough to handle different data types for values (strings, numbers, objects, etc.).
  • The class should be well-documented and easy to use.

Expected Behavior:

  • Adding a value with tags should not modify the value itself.
  • Retrieving values by tags should return a new array containing only the requested values, without modifying the original data.
  • Removing a value should update the internal data structures to reflect the removal.
  • The system should handle cases where a tag is not associated with any values gracefully (e.g., return an empty array).

Edge Cases to Consider:

  • Empty tag strings.
  • Duplicate tags for the same value.
  • Retrieving values with a combination of tags (AND logic).
  • Removing a value that doesn't exist.
  • Handling null or undefined values.

Examples

Example 1:

Input:
const taggedValues = new TaggedValues();
taggedValues.add(10, ['a', 'b']);
taggedValues.add('hello', ['b', 'c']);
taggedValues.add({name: 'world'}, ['a']);

Output: taggedValues.get(['b'])
[10, 'hello']
Explanation: Both 10 and 'hello' are associated with the tag 'b'.

Example 2:

Input:
const taggedValues = new TaggedValues();
taggedValues.add(10, ['a', 'b']);
taggedValues.remove(10, ['a']);

Output: taggedValues.get(['a'])
[10]
taggedValues.get(['b'])
[10]
Explanation: Removing 10 with tag 'a' only removes the association with 'a', not 'b'.

Example 3: (Edge Case - No Values with Tag)

Input:
const taggedValues = new TaggedValues();
taggedValues.add(10, ['a', 'b']);
taggedValues.get(['d'])

Output: []
Explanation: There are no values associated with the tag 'd'.

Constraints

  • The number of values stored in the TaggedValues object can be up to 10,000.
  • Tags are strings and can be up to 32 characters long.
  • The retrieval of values by tags should complete within 100 milliseconds for a dataset of 10,000 values.
  • Input values can be of any JavaScript data type.

Notes

Consider using a data structure like a Map or a Set to efficiently store the tag-value associations. Think about how to handle the AND logic when retrieving values based on multiple tags. Focus on creating a clean and maintainable class with clear methods. The efficiency of your implementation will be a key factor in its success.

Loading editor...
javascript