Pointer Tagging System in JavaScript
Pointer tagging is a technique used to associate metadata with pointers, allowing for runtime analysis and optimization. This challenge asks you to implement a simplified pointer tagging system in JavaScript, enabling you to track the usage frequency of specific variables (represented as strings) within a given scope. This is useful for identifying hot spots in code and potentially optimizing them.
Problem Description
You are tasked with creating a JavaScript object, PointerTracker, that manages a collection of "pointers" (represented as strings) and their associated usage counts. The PointerTracker should provide the following functionalities:
track(pointerName): Registers a new pointer with the given name. If the pointer already exists, this method should do nothing.increment(pointerName): Increments the usage count of the specified pointer. If the pointer doesn't exist, it should first be registered usingtrack(pointerName)and then incremented.getUsage(pointerName): Returns the current usage count of the specified pointer. If the pointer doesn't exist, it should return 0.getTopPointers(n): Returns an array of thenmost frequently used pointers, sorted in descending order of usage count. Ifnis greater than the number of tracked pointers, return all tracked pointers sorted by usage. Ifnis less than or equal to 0, return an empty array.
Examples
Example 1:
Input:
tracker = new PointerTracker();
tracker.track("var1");
tracker.increment("var2");
tracker.increment("var1");
tracker.increment("var2");
tracker.increment("var2");
Output:
tracker.getUsage("var1"); // Returns 2
tracker.getUsage("var2"); // Returns 3
tracker.getUsage("var3"); // Returns 0
Explanation: var1 is used twice, var2 is used three times, and var3 is not tracked, so its usage is 0.
Example 2:
Input:
tracker = new PointerTracker();
tracker.track("a");
tracker.track("b");
tracker.track("c");
tracker.increment("a");
tracker.increment("b");
tracker.increment("c");
tracker.increment("a");
tracker.increment("b");
tracker.increment("a");
Output:
tracker.getTopPointers(2); // Returns ["a", "b"]
Explanation: "a" has a usage count of 3, "b" has a usage count of 2, and "c" has a usage count of 1. The top 2 pointers are "a" and "b".
Example 3: (Edge Case)
Input:
tracker = new PointerTracker();
tracker.getTopPointers(5); // Requesting more pointers than exist
Output:
tracker.getTopPointers(5); // Returns ["a", "b", "c"]
Explanation: Since there are only three tracked pointers, all three are returned, sorted by usage.
Constraints
- Pointer names will be strings.
- Usage counts will be non-negative integers.
ningetTopPointers(n)will be a non-negative integer.- The number of unique pointer names will not exceed 1000.
- Performance:
incrementandgetUsageshould have an average time complexity of O(1).getTopPointersshould have a time complexity of O(n log k), where n is the number of tracked pointers and k is the requested number of top pointers.
Notes
- Consider using a JavaScript object (or Map) to store the pointers and their usage counts.
- For
getTopPointers, you can use JavaScript's built-in sorting methods, but be mindful of the performance constraints. Consider using a min-heap data structure for optimal performance if you want to go beyond the basic requirements. - Error handling is not required for invalid input types (e.g., passing a number as a pointer name). Assume valid input.
- The order of pointers with the same usage count in
getTopPointersis not important.