72 data structures and algorithms questions72 Interview Questions
Determine if a string containing parentheses is valid. A valid string has matching and properly nested opening and closing parentheses.
#1. Valid Parentheses
Imagine a grid where each cell has a value. Your task is to find the number of island regions where the sum of the values in each island is perfectly divisible by a given number K.
#2. Count Islands With Total Value Divisible by K
Imagine you're watering a garden using strategically placed taps. Find the fewest taps you need to open to water the entire garden, given each tap's watering range.
#3. Minimum Number of Taps to Open to Water a Garden
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.
#4. Course Schedule II
Find the three numbers in a given array whose product is maximum. Return the maximum product possible from any three numbers in the array.
#5. Maximum Product of Three Numbers
Imagine a tree where nodes have values. Bob and Alice travel from different starting points to the root. Determine the most profitable path for Bob, considering that Alice can reduce node values on her path.
#6. Most Profitable Path in a Tree
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.
#7. LRU Cache
You are given a string and need to make it an anti-palindrome, where no character at index i equals the character at index n-1-i. Determine the minimum number of operations to achieve this.
#8. Make String Anti-palindrome
Find the length of the longest subsequence within an array of numbers that sums up to a given target value. You need to choose elements from the array (not necessarily contiguous) to achieve the target sum and maximize the number of chosen elements.
#9. Length of the Longest Subsequence That Sums to Target
You're given an array of numbers and a space. Your task is to find the largest number of targets you can destroy sequentially, where destroying a target removes all numbers within a given space of its multiples.
#10. Destroy Sequential Targets
Determine the number of unique substrings that can be formed from a given string. Count each distinct substring only once.
#11. Number of Distinct Substrings in a String
Find the number of substrings in a binary string that have a specific ratio of zeros to ones. The ratio is given as input.
#12. Number of Substrings With Fixed Ratio
You're given several gardens with varying levels of beauty, and a limited budget to improve them. Maximize the total beauty across all gardens by strategically spending your budget to increase individual garden beauty, considering minimum beauty requirements and associated costs.
#13. Maximum Total Beauty of the Gardens
You're given a grid of numbers. Find the maximum difference between the sum of any path from the top-left to bottom-right cell and the sum of all other cells in the grid.
#14. Maximum Difference Score in a Grid
Evaluate an expression string containing variables and basic arithmetic operations. You'll need to handle variable assignments and simplify the resulting expression into a list of terms.
#15. Basic Calculator IV
Imagine a grid of land and water. Find the largest connected area of land and return its size.
#16. Max Area of Island
Fill in the empty cells of a partially completed Sudoku grid according to the game's rules. The goal is to complete the grid so that each row, column, and 3x3 subgrid contains all digits from 1 to 9 without repetition.
#17. Sudoku Solver
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.
#18. Product of Array Except Self
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.
#19. Rotting Oranges
You're given a non-negative integer represented as an array of digits. Add one to the integer and return the updated array.
#20. Plus One