1. [OA] Client-Side Router — Design a Router for Google Maps
Google Maps needs an efficient way to handle client-side routing for various map views. Your task is to design a class-based router that can manage routes based on the current path and dynamically load components.Problem Statement: Implement a class Router that manages routes for a map application and has the following methods:
addRoute(path: string, component: any): void: Adds a new route mapping path to a component.
navigate(path: string): void: Navigates to the specified path and loads the associated component.
getCurrentComponent(): any: Returns the component of the current route.
Example 1: const router = new Router(); router.addRoute('/home', HomeComponent); router.addRoute('/about', AboutComponent); router.navigate('/home'); console.log(router.getCurrentComponent()); // HomeComponent exampleConstraints:
The path will always be a string in the format of /[a-zA-Z]+.
The component can be any function or class that should be rendered for that route.
system designSeniorcaching#2
2. [OA] LRU Cache — Implement a caching layer for Google Search Results
As part of Google Search Services, you need to optimize data retrieval by implementing an LRU (Least Recently Used) Cache. This cache will store recently accessed search queries and their results, helping to reduce the load on Google's servers and speed up user interactions.Problem Statement: Design and implement a data structure for an LRU Cache that supports the following operations:
get(key: number): number: Retrieves the value of the key if the key exists in the cache. Otherwise, returns -1.
put(key: number, value: number): void: Updates the value of the key if the key exists. If the key does not exist, add the key-value pair to the cache. If the cache reaches its capacity, it should invalidate the least recently used item before inserting the new item.