MongoDB software engineer interviews cover algorithms, data structures, system design, and coding problems drawn from real interview rounds.
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^4Sign up for free to access walkthroughs, AI-generated questions, and more.
Get Started Free