Showing 163 questions
Transform a binary tree so that the original left-most leaf becomes the new root. The original root becomes a left child, and the original right child becomes the right child of the original root's parent.
#1. Binary Tree Upside Down
Design a data structure that supports insert, delete, and getRandom operations, all in average O(1) time. The getRandom method should return a random element from the current set of elements.
#2. Insert Delete GetRandom O(1)
In a special binary tree where each node's value is either the root's value or greater, find the second smallest value among all nodes. If no such second minimum exists, return -1.
#3. Second Minimum Node In a Binary Tree
Imagine peeling off the leaves of a binary tree layer by layer. The goal is to return a list of lists, where each inner list contains the values of leaves removed in each step, from the outermost leaves inward.
#4. Find Leaves of Binary Tree
You're given a nested list of integers where each integer has a depth. Calculate the weighted sum of all integers, where the weight is the depth of the integer from the bottom of the nested structure.
#5. Nested List Weight Sum II
You are given a binary tree. Devise a way to convert the tree into a string representation (serialize) and then reconstruct the tree from that string (deserialize).
#6. Serialize and Deserialize Binary Tree
Design a stack that supports push, pop, top, peekMax, and popMax operations, all in O(1) time complexity where possible. The challenge lies in efficiently tracking and retrieving the maximum value within the stack.
#7. Max Stack
In a group of people, find the "celebrity": someone who is known by everyone else, but doesn't know anyone themselves. You are given a way to query whether two people know each other.
#8. Find the Celebrity
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.
#9. Word Ladder
You are given a nested list of integers where each integer has a depth. Calculate the weighted sum of all integers in the list, where the weight of an integer is its depth within the nested structure.
#10. Nested List Weight Sum
Find the contiguous subarray within a given array of numbers which has the largest sum. Return the sum of this subarray.
#11. Maximum Subarray
You're given a list of words and need to efficiently find the shortest distance between two specified words within that list. Unlike a simpler version, you'll be asked to do this repeatedly for different word pairs on the *same* word list.
#12. Shortest Word Distance II
Determine if a string containing parentheses is valid. A valid string has matching and properly nested opening and closing parentheses.
#13. Valid Parentheses
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
Find the contiguous subarray within a given array of numbers which has the largest product. The array can contain negative numbers, so you need to account for both maximum and minimum values.
#15. Maximum Product Subarray
You're given a flowerbed represented by an array of 0s and 1s, where 0 means empty and 1 means planted. Determine if you can plant a given number of new flowers without violating the rule that adjacent plots cannot be planted.
#16. Can Place Flowers
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
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.
#18. Pow(x, n)
Determine if an array of numbers can be split into K non-empty subsets with equal sums. Essentially, can you divide the numbers into K groups where each group has the same total?
#19. Partition to K Equal Sum Subsets
Given a sorted array, find the first and last position of a given target value. If the target is not found, return [-1, -1].
#20. Find First and Last Position of Element in Sorted Array