MongoDB software engineer interviews cover algorithms, data structures, system design, and coding problems drawn from real interview rounds.
Thread-Safe Connection Pool Design a thread-safe connection pool that limits the number of concurrent database connections. The pool should reuse...
Input: Integer(s)documentId, accessCount, and timestamp. Write a function that takes in a collection of documents and returns a list of the top N documents with the highest access counts during the specified period.def top_accessed_documents(documents: List[Dict[str, Union[str, int]]], start_time: str, end_time: str, N: int) -> List[Dict[str, int]]:, start_time = '2023-09-01T00:00:00Z', end_time = '2023-09-02T23:59:59Z', N = 2 had the highest access count followed by doc1., start_time = '2023-09-01T00:00:00Z', end_time = '2023-09-01T23:59:59Z', N = 1i, you cannot select elements at indices i-1 or i+1.def max_non_adjacent_sum(values: List[int]) -> int:[3, 2, 5, 10, 7] 15 [5, 1, 1, 5] 10 values will be between 1 and 1000.values will be between -1000 and 1000.customers table with thousands of records. The original query takes too long to execute. Your goal is to optimize this query. You need to explain possible optimizations, such as adding indexes, refining the query structure, or altering filter conditions to reduce execution time. optimize_query(original_query: str) -> str"SELECT * FROM customers WHERE country = 'USA' AND age > 30;""CREATE INDEX idx_country_age ON customers (country, age); SELECT * FROM customers WHERE country = 'USA' AND age > 30;"country and age speeds up the lookup."SELECT * FROM customers WHERE last_purchase_date < '2022-01-01';""CREATE INDEX idx_last_purchase ON customers (last_purchase_date); SELECT * FROM customers WHERE last_purchase_date < '2022-01-01';"last_purchase_date column enhances query performance for historical lookups.original_query length should not exceed 500 characters.customers table can have millions of records.def most_frequent_element(elements: List[Union[int, str]]) -> Union[int, str]:['apple', 'banana', 'apple', 'orange', 'banana', 'banana'] 'banana' [1, 3, 1, 2, 2, 3, 3] 3 len(elements) <= 10^5 elements can contain integers and strings only.operations (a list of objects) where each object has properties: type (string, can be 'read' or 'write'), dataSize (int, the size of the operation in bytes), and timestamp (string, timestamp of the operation in ISO format). The function should identify which operations are taking the longest based on their timestamps and data sizes, returning a list of operations that exceed a certain thresholdTime (in milliseconds).def troubleshoot_slow_db(operations: List[Dict[str, Union[str, int]]], thresholdTime: int) -> List[Dict[str, Union[str, int]]]:operations = [{'type': 'read', 'dataSize': 200, 'timestamp': '2023-10-05T12:00:00Z'}, {'type': 'write', 'dataSize': 500, 'timestamp': '2023-10-05T12:00:02Z'}] thresholdTime = 1000 [{'type': 'write', 'dataSize': 500, 'timestamp': '2023-10-05T12:00:02Z'}] operations = [{'type': 'read', 'dataSize': 150, 'timestamp': '2023-10-05T12:00:00Z'}, {'type': 'write', 'dataSize': 300, 'timestamp': '2023-10-05T12:00:01Z'}] thresholdTime = 500 [] 1 <= len(operations) <= 100 1 <= dataSize <= 1000 timestamp format is always valid.documentId, write a function to determine the documentId that was accessed the most frequently. If there are ties, return any of the most frequent documentIds.def most_frequent_access(logs: List[int]) -> int:logs = [1, 2, 2, 3, 1, 1, 4] 1 logs = [5, 5, 6, 6, 7] 5 or 6 1 <= len(logs) <= 10^5 0 <= logs[i] <= 10^9 logs are non-negative integers.integers, return the list without duplicates. def remove_duplicates(nums: List[int]) -> List[int]: - Returns a filtered list of unique numbers.0 <= len(nums) <= 10^4-10^4 <= nums[i] <= 10^4string and a start and end time defining the window, return a list of timestamps that fall within this range. def fetch_timestamps(timestamps: List[str], start: str, end: str) -> List[str]: - Returns a filtered list of timestamps.1 <= len(timestamps) <= 10^4timestamps[i] is in ISO 8601 format.nums, return the index of the pivot where the rotation occurs.def find_pivot(nums: List[int]) -> int: Returns the pivot index.[4,5,6,7,0,1,2]33.Example 2:[1]01 <= nums.length <= 5000-10^5 <= nums[i] <= 10^5.s, return the length of the longest substring without repeating characters.def length_of_longest_substring(s: str) -> int: Returns the length of the longest substring."abcabcbb"3"abc", with the length being 3.Example 2:"bbbbb"1"b", with the length being 1.Constraints:0 <= s.length <= 50000s consists of English letters, digits, symbols, and spaces.RateLimiter that allows requests to be allowed to pass if they fall within a defined rate limit.class RateLimiter:def allow_request(self, user_id: str) -> bool: - Returns True if the request is allowed.def get_request_count(self, user_id: str) -> int: - Returns the count of requests made by the user.1 <= requests_per_window <= 10^31 <= window_size <= 10^31 <= len(user_id) <= 100.JobQueue that allows adding jobs and processing them in concurrent threads. class JobQueue: def add_job(self, job: Callable) -> None: - Adds a job to the queue.def process_jobs(self, num_threads: int) -> None: - Starts processing jobs using specified threads.def get_status(self) -> List[str]: - Returns a list of job statuses ('pending', 'completed').1 <= num_threads <= 10LRUCache that supports get and put operations.class LRUCache:def __init__(self, capacity: int): Initializes the cache with a specific capacity.def get(self, key: int) -> int: Returns the value of the key if present, otherwise return -1.def put(self, key: int, value: int) -> None: Updates or inserts the value if the key is not already present. When the cache reaches its capacity, it should invalidate the least recently used item before inserting a new item.1 <= capacity <= 300000 <= key, value <= 10^4.MongoDBApi that simulates these operations on a collection.class MongoDBApi:def create(self, data: Dict[str, Any]) -> str:: Creates a new record and returns its ID.def read(self, record_id: str) -> Dict[str, Any]: Returns the record specified by record_id.def update(self, record_id: str, data: Dict[str, Any]) -> bool: Updates the record and returns success status.def delete(self, record_id: str) -> bool: Deletes the record and returns success status.record_id is a string with a length of up to 36 characters.Sign up for free to access walkthroughs, AI-generated questions, and more.
Get Started Free