Untitled
user_4319476
plain_text
16 days ago
5.7 kB
2
Indexable
Never
Excellent question. To solve this space-efficient version, we can modify our approach as follows: Replace TreeMap with a fixed-size array or circular buffer Use binary search for efficient retrieval Implement a custom eviction policy Here's a possible implementation in Java: class TimeMap { private static final int N = 5; // Number of recent values to keep per key private Map<String, TimestampedValue[]> map; private static class TimestampedValue { int timestamp; String value; TimestampedValue(int timestamp, String value) { this.timestamp = timestamp; this.value = value; } } public TimeMap() { map = new HashMap<>(); } public void set(String key, String value, int timestamp) { TimestampedValue[] values = map.computeIfAbsent(key, k -> new TimestampedValue[N]); int index = findInsertionIndex(values, timestamp); if (index < N) { // Shift elements if necessary System.arraycopy(values, index, values, index + 1, Math.min(N - index - 1, values.length - index - 1)); values[index] = new TimestampedValue(timestamp, value); } } public String get(String key, int timestamp) { TimestampedValue[] values = map.get(key); if (values == null) return ""; int index = findFloorIndex(values, timestamp); return index >= 0 ? values[index].value : ""; } private int findInsertionIndex(TimestampedValue[] values, int timestamp) { int left = 0, right = N - 1; while (left <= right) { int mid = left + (right - left) / 2; if (values[mid] == null || values[mid].timestamp < timestamp) { left = mid + 1; } else { right = mid - 1; } } return left; } private int findFloorIndex(TimestampedValue[] values, int timestamp) { int left = 0, right = N - 1; while (left <= right) { int mid = left + (right - left) / 2; if (values[mid] == null || values[mid].timestamp > timestamp) { right = mid - 1; } else { left = mid + 1; } } return right; } } Excellent question. To solve this space-efficient version, we can modify our approach as follows: Replace TreeMap with a fixed-size array or circular buffer Use binary search for efficient retrieval Implement a custom eviction policy Here's a possible implementation in Java: javaCopyclass TimeMap { private static final int N = 5; // Number of recent values to keep per key private Map<String, TimestampedValue[]> map; private static class TimestampedValue { int timestamp; String value; TimestampedValue(int timestamp, String value) { this.timestamp = timestamp; this.value = value; } } public TimeMap() { map = new HashMap<>(); } public void set(String key, String value, int timestamp) { TimestampedValue[] values = map.computeIfAbsent(key, k -> new TimestampedValue[N]); int index = findInsertionIndex(values, timestamp); if (index < N) { // Shift elements if necessary System.arraycopy(values, index, values, index + 1, Math.min(N - index - 1, values.length - index - 1)); values[index] = new TimestampedValue(timestamp, value); } } public String get(String key, int timestamp) { TimestampedValue[] values = map.get(key); if (values == null) return ""; int index = findFloorIndex(values, timestamp); return index >= 0 ? values[index].value : ""; } private int findInsertionIndex(TimestampedValue[] values, int timestamp) { int left = 0, right = N - 1; while (left <= right) { int mid = left + (right - left) / 2; if (values[mid] == null || values[mid].timestamp < timestamp) { left = mid + 1; } else { right = mid - 1; } } return left; } private int findFloorIndex(TimestampedValue[] values, int timestamp) { int left = 0, right = N - 1; while (left <= right) { int mid = left + (right - left) / 2; if (values[mid] == null || values[mid].timestamp > timestamp) { right = mid - 1; } else { left = mid + 1; } } return right; } } Key points of this solution: Space Efficiency: Uses a fixed-size array (N = 5) for each key instead of a TreeMap. Automatically evicts old entries when new ones are added. Time Complexity: set(): O(log N) for binary search + O(N) for array shift in worst case, but since N is small and constant, it's effectively O(1). get(): O(log N) due to binary search. Eviction Policy: When a new entry is added and the array is full, the oldest entry is automatically removed. Binary Search: Used for both insertion and retrieval to maintain O(log N) time complexity. This solution addresses the space efficiency requirement by limiting the number of values stored per key. It maintains the required time complexity for operations and automatically handles the eviction of old data. Trade-offs: We lose the ability to access very old data, but gain significant space efficiency. The set operation might be slightly slower due to array shifting, but for small N, this should be negligible. In an interview, you could discuss these trade-offs and potential further optimizations, such as using a circular buffer to avoid shifting elements.
Leave a Comment