Canonical software engineer interviews cover algorithms, data structures, system design, and coding problems drawn from real interview rounds.
get and put. The get(key) method returns the value of the key if the key exists in the cache, otherwise returns -1. The put(key, value) method updates the value of the key if the key already exists and if the cache reaches its capacity, it should invalidate the least recently used item before inserting a new item. Your implementation should optimize for time complexity of O(1) for both operations.Function/class signature:class LRUCache:def __init__(self, capacity: int): def get(self, key: int) -> int: def put(self, key: int, value: int) -> None:cache = LRUCache(2) cache.put(1, 1) cache.put(2, 2) output1 = cache.get(1) # returns 1 cache.put(3, 3) # evicts key 2 output2 = cache.get(2) # returns -1 (not found)
1, -1
cache = LRUCache(1) cache.put(2, 1) output1 = cache.get(2) # returns 1 cache.put(3, 2) # evicts key 2 output2 = cache.get(2) # returns -1 (not found)
1, -1
1 <= capacity <= 3000 0 <= key <= 10000 0 <= value <= 10000 get and put operation will be called at most 10^4 times.find_resource_leaks that takes a list of system logs (strings) as input. Each log entry contains a timestamp and the resource usage details. The function should identify which resources (e.g., CPU, Memory) exceed a predefined threshold consistently over multiple log entries and return them as a list of resources that have potential leaks.def find_resource_leaks(logs: List[str], threshold: Dict[str, int]) -> List[str]:find_resource_leaks(["2023-10-01T12:00:00 CPU:50 Memory:200", "2023-10-01T12:01:00 CPU:70 Memory:250"], {"CPU": 60, "Memory": 225})['CPU']find_resource_leaks(["2023-10-01T12:00:00 CPU:30 Memory:100", "2023-10-01T12:01:00 CPU:20 Memory:90"], {"CPU": 25, "Memory": 95})[]def __init__(self, k: int) -> None def add(self, num: int) -> None def kth_largest(self) -> intkth_largest = KthLargest(3) kth_largest.add(3) kth_largest.add(5) kth_largest.add(10) kth_largest.add(9) kth_largest.add(4)
5 kth_largest = KthLargest(1) kth_largest.add(3) kth_largest.add(5) kth_largest.add(10)
10 1 <= k <= 1000 -10^4 <= num <= 10^4 10^4 calls will be made to add.def longest_unique_substring(s: str) -> int:"snapcraft" 8 "ubuntuubuntu" 7 "ubnto" (length 7). Constraints:1 <= len(s) <= 1000s consists of only lowercase English letters.s, implement a function that finds the frequency of each character in the string and returns a dictionary with characters as keys and their corresponding counts as values. The function needs to handle both uppercase and lowercase letters as case sensitive.def count_character_frequencies(s: str) -> dict:"Hello World"{'H': 1, 'e': 1, 'l': 3, 'o': 2, ' ': 1, 'W': 1, 'r': 1, 'd': 1} "Canonical"{'C': 1, 'a': 2, 'n': 2, 'o': 1, 'i': 1, 'c': 1, 'l': 1} 1 <= len(s) <= 10^4 Sign up for free to access walkthroughs, AI-generated questions, and more.
Get Started Free