easy

Maximum Consecutive Floors Without Special Floors

February 6, 2024
easy
DFS

Problem Statement # You are designing an algorithm for an elevator system in a skyscraper without accessing specific floors marked as special. Given a list of special floors specialFloors and two integers bottom and top indicating the lowest and highest floors you’re allowed to visit, return the maximum number of consecutive floors you can visit between bottom and top without stopping at any special floor. Special floors are guaranteed to be within the range of bottom to top. ...

Random element from the stream with uniform probability

February 6, 2024
easy
Reservoir Sampling

Problem # Given a stream of elements too large to store in memory, pick a random element from the stream with uniform probability. Solution Approach # The task described is a classic problem in computer science, often solved using a technique known as Reservoir Sampling. The specific case of picking a single random element from a stream can be considered as Reservoir Sampling with a reservoir size of 1. Here’s how to approach the problem: ...

Optimal Task Assignment

February 4, 2024
easy
array

Problem Statement # You are given a list of tasks where each task has a duration. Your goal is to assign these tasks to workers so that all tasks are completed while minimizing the total time. Each worker must work on exactly two tasks and can only work on one task at a time. Return the optimal assignment of tasks. Example: Input: tasks = [6, 3, 2, 7, 5, 5] Output: [(2, 7), (3, 6), (5, 5)] Explanation: Pairing the tasks in this way results in the minimum total duration: 9, 9, and 10. ...

Validate Stack Sequences

January 26, 2024
easy
Stack

Problem Statement # Given two sequences pushed and popped, each with distinct values, return true if this could have been the result of a sequence of push and pop operations on an initially empty stack, otherwise false. Example: Input: pushed = [1,2,3,4,5], popped = [4,5,3,2,1] Output: True Explanation: We can push 1, 2, 3, 4, and 5 and then pop them in the order [4,5,3,2,1]. Solution Approach # The solution involves simulating the push and pop operations on a stack. ...

Minimum Cost to Connect Sticks

January 23, 2024
easy
heap

Problem Statement # You have some sticks with positive integer lengths. You can connect any two sticks of lengths X and Y into one stick by paying a cost of X + Y. You must connect all the sticks until there is only one stick remaining. Return the minimum cost to connect all the given sticks into one stick in this way. Example: Input: sticks = [2, 4, 3] Output: 14 Explanation: You start with sticks = [2,4,3]. ...

Distribute Candies to People

January 19, 2024
easy
Simulation

Problem # You have n candies, and you want to distribute them to a group of people arranged in a circle in the following way: Start at the first person (index 0), and then move to the next on the right for each distribution. In the first round, give 1 candy to the first person, 2 candies to the next person, and so on, until you run out of candies. ...

Maximum sum of a contiguous subarray

January 1, 2024
easy

Problem # Given a list of integers, find the maximum sum of a contiguous subarray with length 3. Solution # Here’s the Python code to find the maximum sum of a contiguous subarray of length 3 in a given list of integers: def maxSumSubarrayOfSizeThree(nums): """ Find the maximum sum of a contiguous subarray of size 3. Args: nums (List[int]): A list of integers. Returns: int: The maximum sum of a contiguous subarray of size 3. ...

Second Largest Number

January 1, 2024
easy

Problem # Given an array of integers, write an algorithm that returns the second largest number in the array. If there is no second largest number, return -1. Solution # Here’s the Python code to find the second largest number in an array of integers: def secondLargest(nums): """ Find the second largest number in an array of integers. Args: nums (List[int]): The array of integers. Returns: int: The second largest number in the array, or -1 if it doesn't exist. ...

Print the nodes in a binary tree level-wise

December 24, 2023
easy
BFS

Problem # Print the nodes in a binary tree level-wise. For example, the following should print 1, 2, 3, 4, 5. 1 / \ 2 3 / \ 4 5 Solution # Here is the Python code to print the nodes of a binary tree level-wise: class Node: def __init__(self, key): self.left = None self.right = None self.val = key def printLevelOrder(root): if not root: return queue = [] queue. ...

Intersection Area

December 12, 2023
easy

Problem # Given two rectangles on a 2D graph, return the area of their intersection. If the rectangles don’t intersect, return 0. For example, given the following rectangles: { "top_left": (1, 4), "dimensions": (3, 3) # width, height } and { "top_left": (0, 5), "dimensions": (4, 3) # width, height } return 6. solution # def calculate_intersection_area(rect1, rect2): """ Calculate the area of intersection of two rectangles. Each rectangle is represented as a dictionary with 'top_left' and 'dimensions' keys. ...