Showing 75 questions
Find two numbers in a given array that add up to a specific target value, and return their indices. Essentially, you need to efficiently locate the pair that satisfies the sum requirement.
#1. Two Sum
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.
#2. LRU Cache
You are given a collection of intervals. Merge all overlapping intervals into a single interval and return the result.
#3. Merge Intervals
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.
#4. Best Time to Buy and Sell Stock
Find the length of the longest substring within a given string that contains no repeating characters. The substring must be contiguous.
#5. Longest Substring Without Repeating Characters
Determine if a string containing parentheses is valid. A valid string has matching and properly nested opening and closing parentheses.
#6. Valid Parentheses
Given a grid representing land and water, count the number of distinct islands. An island is formed by connected land cells.
#7. Number of Islands
Given a list of words, group together all the anagrams. Anagrams are words that contain the same letters rearranged.
#8. Group Anagrams
Given an array representing the height of bars, calculate how much water can be trapped between them after rain. Think about finding the maximum height to the left and right of each bar.
#9. Trapping Rain Water
Find the longest palindromic substring within a given string. A palindromic substring reads the same forwards and backward.
#10. Longest Palindromic Substring
Find the k most frequent elements in a given array of numbers. Return the top k elements based on their frequency.
#11. Top K Frequent Elements
Find the median of two sorted arrays. The arrays can be of different sizes, and you should aim for an efficient solution.
#12. Median of Two Sorted Arrays
Find all unique triplets in an array that sum to zero. You'll need to consider the potential for duplicate numbers and avoid duplicate triplets in your solution.
#13. 3Sum
Determine the total number of continuous subarrays within a given array that sum up to a specific target value, k.
#14. Subarray Sum Equals K
You are given k sorted lists. Combine all of their elements into a single sorted list and return it.
#15. Merge k Sorted Lists
Find the longest string that is a prefix of all strings in a given array. If no common prefix exists, return an empty string.
#16. Longest Common Prefix
You're given a sorted array that has been rotated, and a target value. Find the index of the target value in the rotated array, or return -1 if it's not present.
#17. Search in Rotated Sorted Array
Find the contiguous subarray within a given array of numbers which has the largest sum. Return the sum of this subarray.
#18. Maximum Subarray
You are given a square matrix representing an image. Rotate the image by 90 degrees clockwise in-place.
#19. Rotate Image
Traverse a given matrix in a spiral pattern, returning all elements in the order you visit them. Essentially, walk around the matrix layer by layer, adding elements to a list.
#20. Spiral Matrix
You are given two linked lists, each representing a non-negative integer. Add the two numbers and return the sum as a linked list.
#21. Add Two Numbers
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.
#22. Kth Largest Element in an Array
Given a number 'n', generate all possible well-formed (balanced) parentheses strings of length 2*n. For instance, if n=3, you need to create all valid combinations like '((()))', '(()())', etc.
#23. Generate Parentheses
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.
#24. Rotting Oranges
Given a sorted array, find the first and last position of a given target value. If the target is not found, return [-1, -1].
#25. Find First and Last Position of Element in Sorted Array
You are given two sorted integer arrays. Merge the second array into the first one, maintaining the sorted order.
#26. Merge Sorted Array
Convert a Roman numeral string into its corresponding integer value. The input string will represent a valid Roman numeral.
#27. Roman to Integer
You are a robber planning to rob houses along a street. Maximize your loot, but be careful - adjacent houses have connected security systems that will trigger an alarm if you rob them both.
#28. House Robber
Determine whether an integer is a palindrome. That is, it reads the same forwards and backward.
#29. Palindrome Number
Given an array, rearrange it so that all zeros are moved to the end while maintaining the relative order of the non-zero elements.
#30. Move Zeroes
You are given an array representing the heights of vertical lines. Find two lines that, together with the x-axis, form a container that holds the most water.
#31. Container With Most Water
You are given a list of meeting time intervals. Determine the minimum number of conference rooms required to accommodate all the meetings without any overlap.
#32. Meeting Rooms II
Given a set of coin denominations and a target amount, find the minimum number of coins needed to reach that amount. You can assume you have an unlimited supply of each coin type.
#33. Coin Change
Determine if two strings are anagrams of each other, meaning they contain the same characters in a different order. You need to check if one string can be rearranged to form the other.
#34. Valid Anagram
Calculate a new array where each element is the product of all numbers in the original array, excluding the number at that index. Do this efficiently without using division.
#35. Product of Array Except Self
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. These consecutive numbers do not need to be adjacent in the original array.
#36. Longest Consecutive Sequence
Koko wants to eat all the bananas in some piles within a certain time. Find the minimum eating speed that allows her to finish on time.
#37. Koko Eating Bananas
You are given an array where each element represents your maximum jump length at that position. Determine if you can reach the last index starting from the first index.
#38. Jump Game
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.
#39. Integer to Roman
Determine if it is possible to finish all courses given prerequisite relationships between them. Essentially, detect if a cycle exists in the course dependencies.
#40. Course Schedule
Find the smallest substring within a larger string that contains all the characters of a target string. You need to locate the minimum window that satisfies this condition.
#41. Minimum Window Substring
You are given the head of a singly linked list. Reverse the list and return the new head.
#42. Reverse Linked List
Given a sequence of numbers, find the next lexicographically greater permutation of the numbers. If no greater permutation exists, rearrange the numbers into the smallest possible permutation (ascending order).
#43. Next Permutation
Find the maximum value within each sliding window of a given size as it moves across an array.
#44. Sliding Window Maximum
You're given a list of courses and their prerequisites. Determine a valid order in which you can take all the courses, respecting the dependencies. If it's impossible to finish all courses, indicate that.
#45. Course Schedule II
You're given two sorted lists. Combine them into a single sorted list containing all elements from both original lists.
#46. Merge Two Sorted Lists
Determine if a given string is a palindrome, considering only alphanumeric characters and ignoring cases. Essentially, check if the string reads the same forwards and backward after removing non-alphanumeric characters and converting to lowercase.
#47. Valid Palindrome
Given a sorted array, remove the duplicate elements in-place such that each unique element appears only once. Return the new length of the array.
#48. Remove Duplicates from Sorted Array
Find a peak element in an array, where a peak is greater than its neighbors. You only need to return the index of any one peak element if multiple exist.
#49. Find Peak Element
Transform one word into another, one letter at a time, using only words from a given dictionary. Find the shortest sequence of words to reach the target.
#50. Word Ladder
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.
#51. Best Time to Buy and Sell Stock II
Given a set of distinct integers, your task is to generate all possible subsets (the power set). The solution should not contain duplicate subsets.
#52. Subsets
Given an array, rotate the elements to the right by a specified number of steps. The goal is to modify the original array in-place with the rotated elements.
#53. Rotate Array
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. You need to implement a stack with an additional function to efficiently get the minimum value.
#54. Min Stack
Determine if a given string can be segmented into a space-separated sequence of words from a dictionary. You are given a string and a dictionary of words; can you break the string down using words from the dictionary?
#55. Word Break
Determine if a given word can be constructed from letters in a 2D board, where letters can be used horizontally or vertically. Each letter can only be used once in the word.
#56. Word Search
Determine if any value appears at least twice in an array of numbers. Return true if duplicates exist; otherwise, return false.
#57. Contains Duplicate
Find the length of the longest strictly increasing subsequence within a given array of numbers. A subsequence doesn't need to be contiguous, but the elements must maintain their original order.
#58. Longest Increasing Subsequence
Rearrange the characters in a given string such that no two adjacent characters are the same. If such an arrangement is possible, return it; otherwise, return an empty string.
#59. Reorganize String
You are climbing a staircase with n steps. Find how many distinct ways you can climb to the top, given that you can climb one or two steps at a time.
#60. Climbing Stairs
Compress a given string by replacing consecutive repeating characters with the character followed by the count of repetitions. If the compressed string isn't shorter, return the original.
#61. String Compression
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.
#62. Sort Colors
You are given an array where each element represents the maximum jump length from that position. Find the minimum number of jumps required to reach the last index.
#63. Jump Game II
The problem involves decoding a string that follows a specific encoding rule: k[encoded_string], where the encoded_string inside the square brackets is repeated exactly k times. Your task is to decode the string and return the decoded string.
#64. Decode String
Find the maximum sum of any path in a binary tree, where a path can start and end at any node. The path must contain at least one node.
#65. Binary Tree Maximum Path Sum
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.
#66. Copy List with Random Pointer
Determine if a given 9x9 Sudoku board is valid. Check if each row, column, and 3x3 subgrid contains the digits 1-9 without repetition.
#67. Valid Sudoku
Given a string of digits, return all possible letter combinations that the number could represent, using the typical phone keypad mapping. The goal is to generate all valid combinations in any order.
#68. Letter Combinations of a Phone Number
Given a list of daily temperatures, find how many days you have to wait until a warmer temperature. Return a list where each element represents the waiting days for each day's temperature.
#69. Daily Temperatures
Distribute candies to children standing in a line such that each child with a higher rating gets more candies than their neighbors. Find the minimum number of candies needed.
#70. Candy
Calculate x raised to the power of n. Be mindful of potential edge cases and optimize for efficiency, as a naive approach might not be sufficient.
#71. Pow(x, n)
Design and implement a Least Frequently Used (LFU) cache. The cache should efficiently store key-value pairs, evicting the least frequently used items when it reaches capacity.
#72. LFU Cache
Given an absolute file path (Unix style), simplify it by resolving redundant separators, directory names, and navigating back up the directory structure. Return the simplified canonical path.
#73. Simplify Path
Implement a basic calculator to evaluate a simple expression string. The expression can contain parentheses, addition, and subtraction.
#74. Basic Calculator
Find the element that appears more than n/2 times in a given array of size n. Essentially, you need to identify the majority element.
#75. Majority Element