Showing 37 questions
Design a data structure that supports insertion, deletion, and random element retrieval, all in constant time on average. You'll need to handle edge cases carefully to achieve O(1) complexity.
#1. Insert Delete GetRandom O(1)
Given a grid representing land and water, count the number of distinct islands. An island is formed by connected land cells.
#2. Number of Islands
Design a data structure that efficiently stores a set of strings and allows for prefix-based searches. Implement methods to insert strings, search for complete words, and check if any word starts with a given prefix.
#3. Implement Trie (Prefix Tree)
You're given a linked list where each node has a regular 'next' pointer and a 'random' pointer. Create a deep copy of this list, preserving both the next and random pointer relationships.
#4. Copy List with Random Pointer
You're given the coordinates of several points. Determine the maximum area of any triangle that can be formed using these points.
#5. Find Maximum Area of a Triangle
Design a system to manage user authentication tokens. Implement features to generate, renew, and count unexpired tokens, while handling token expiration.
#6. Design Authentication Manager
You are given a grid of oranges, some fresh, some rotten. Rotten oranges contaminate adjacent fresh oranges; find the minimum time until all oranges are rotten.
#7. Rotting Oranges
Find the k most frequent elements in a given array of numbers. Return the top k elements based on their frequency.
#8. Top K Frequent Elements
You are given a collection of intervals. Merge all overlapping intervals into a single interval and return the result.
#9. Merge Intervals
Implement a search suggestion system. Given a search word and a list of product names, return the top 3 product suggestions after each character of the search word is typed.
#10. Search Suggestions System
Design a data structure that efficiently finds the median of a dynamically growing stream of numbers. You need to implement methods to add new numbers and quickly retrieve the current median.
#11. Find Median from Data Stream
You're given an array containing only 0s, 1s, and 2s. Sort the array in place so that elements of the same value are grouped together.
#12. Sort Colors
Given a column title as appears in an Excel sheet, return its corresponding column number. For example, 'A' corresponds to 1, 'B' to 2, 'AA' to 27, and so on.
#13. Excel Sheet Column Number
Convert a given integer into its Roman numeral representation. You need to understand the mapping between integer values and Roman symbols to perform the conversion accurately.
#14. Integer to Roman
Find the length of the longest substring within a given string that contains no repeating characters. The substring must be contiguous.
#15. Longest Substring Without Repeating Characters
Find the inorder successor of a given node in a Binary Search Tree (BST). The inorder successor is the node with the smallest key greater than the given node's key.
#16. Inorder Successor in BST
Imagine two islands represented by ones in a grid of zeros and ones. Find the shortest path to connect the two islands by changing zeros to ones.
#17. Shortest Bridge
Decode a chemical formula to count the number of atoms of each element. The formula may contain nested parentheses and numbers indicating counts.
#18. Number of Atoms
Find the length of the longest substring containing the same character you can obtain after performing at most k replacements of characters in the given string. You can only replace characters with other characters.
#19. Longest Repeating Character Replacement
Determine if a given string can be rearranged to form a palindrome. Essentially, you need to check if the string has at most one character with an odd frequency.
#20. Palindrome Permutation