Showing 579 questions
Design a data structure that acts as a Least Recently Used (LRU) cache. Implement methods to get and put key-value pairs, evicting the least recently used entry when the cache is full.
#1. LRU Cache
You're given daily stock prices. Find the maximum profit you can make by buying and selling the stock once. You must buy before you sell.
#2. Best Time to Buy and Sell Stock
Find the k most frequent elements in a given array of numbers. Return the top k elements based on their frequency.
#3. Top K Frequent Elements
Maximize your profit by buying and selling a stock multiple times. You're given an array representing stock prices, and can make as many transactions as you want.
#4. Best Time to Buy and Sell Stock II
Find the longest string that is a prefix of all strings in a given array. If no common prefix exists, return an empty string.
#5. Longest Common Prefix
Determine if a string containing parentheses is valid. A valid string has matching and properly nested opening and closing parentheses.
#6. Valid Parentheses
You are given k sorted lists. Combine all of their elements into a single sorted list and return it.
#7. Merge k Sorted Lists
Find the contiguous subarray within a given array of numbers which has the largest sum. Return the sum of this subarray.
#8. Maximum Subarray
Find the maximum profit you can make by performing at most two transactions on a given stock price array. You can buy and sell a stock only once at a time.
#9. Best Time to Buy and Sell Stock III
You are given a collection of intervals. Merge all overlapping intervals into a single interval and return the result.
#10. Merge Intervals
You're given stock prices for multiple days. Determine the maximum profit you can achieve by making at most 'k' transactions (buy and sell) within the given timeframe.
#11. Best Time to Buy and Sell Stock IV
Find the kth largest element in an unsorted array. Note that it is the kth largest element in sorted order, not the kth distinct element.
#12. Kth Largest Element in an Array
You are given a square matrix representing an image. Rotate the image by 90 degrees clockwise in-place.
#13. Rotate Image
Given a grid representing land and water, count the number of distinct islands. An island is formed by connected land cells.
#14. Number of Islands
You are given two arrays: numbers to insert and their corresponding indices. Construct a target array by inserting each number at its specified index.
#15. Create Target Array in the Given Order
Determine which kids will have the greatest number of candies after distributing extra candies to each of them. Return a boolean array indicating if each kid has the greatest number of candies.
#16. Kids With the Greatest Number of Candies
Find the two largest numbers in an array. Return the maximum product you can get by subtracting 1 from each of them.
#17. Maximum Product of Two Elements in an Array
You are given two binary trees. Merge them into a new binary tree where nodes at the same position are summed; if only one tree has a node in a position, use its value.
#18. Merge Two Binary Trees
You're given two strings. Merge them by alternating characters from each string, starting with the first string.
#19. Merge Strings Alternately
Write a program that prints numbers from 1 to n, replacing multiples of 3 with "Fizz", multiples of 5 with "Buzz", and multiples of both with "FizzBuzz".
#20. Fizz Buzz