Showing 50 questions
You are given a string and a budget for removing characters. Your goal is to compress the string as much as possible after removing at most k characters. Find the length of the shortest possible compressed string.
#1. String Compression III
You are given dimensions of a grid and constraints on the number of cells you can select and the maximum side length of a selected square. Find the maximum number of ones you can cover by selecting squares within the grid.
#2. Maximum Number of Ones
You are given the head of a singly linked list. Reverse the list and return the new head.
#3. Reverse Linked List
You are given a square matrix representing an image. Rotate the image by 90 degrees clockwise in-place.
#4. Rotate Image
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.
#5. LRU Cache
You're given a linked list, and need to remove the node that is N nodes away from the end. Be careful to handle edge cases like removing the head!
#6. Remove Nth Node From End of List
You're given an integer; reverse the order of its bits. Treat the integer as an unsigned 32-bit number.
#7. Reverse Bits
Given a grid representing land and water, count the number of distinct islands. An island is formed by connected land cells.
#8. Number of Islands
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.
#9. Two Sum
Find the middle node of a given linked list. If the list has an even number of nodes, return the second middle node.
#10. Middle of the Linked List
You are given two sorted integer arrays. Merge the second array into the first one, maintaining the sorted order.
#11. Merge Sorted Array
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.
#12. Majority Element
Reverse the order of each pair of adjacent nodes in a linked list. Return the modified list.
#13. Swap Nodes in Pairs
Determine if a string containing parentheses is valid. A valid string has matching and properly nested opening and closing parentheses.
#14. Valid Parentheses
Determine the number of '1' bits present in the binary representation of a given integer. Essentially, count how many bits are set to one.
#15. Number of 1 Bits
Given an array of integers, determine how many pairs of numbers in the array have a product that is divisible by a given integer k. Count all such pairs and return the total.
#16. Count Array Pairs Divisible by K
Simulate a basic memory allocator. You'll need to allocate blocks of memory, track their IDs, and free them based on their ID.
#17. Design Memory Allocator
Determine whether an integer is a palindrome. That is, it reads the same forwards and backward.
#18. Palindrome Number
Find the contiguous subarray within a given array of numbers which has the largest sum. Return the sum of this subarray.
#19. Maximum Subarray
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.
#20. Pow(x, n)