Logger Rate Limiter Solution (LeetCode)
This question is from the LeetCode Challenge (Aug-2020). See the original question here Design a logger system that receive a stream of messages along with its timestamps, each message should be printed if and only if it is not printed in the last 10 seconds. Given a message and a timestamp (in seconds granularity), return […]
Fitting Shelves Problem
Given the length of a wall w and infinite shelves of two lengths small and large (large > small). Find the optimal solution to put the shelves in the wall such that the empty space left is minimum. We have to use the large shelves as much as possible (keeping the empty space minimum) because […]
Assign Mice to Holes
There are N Mice and N holes placed in a straight line. Each hole can accommodate only 1 mouse. A mouse can stay at his position, move one step right from x to x + 1, or move one step left from x to x − 1. Any of these moves consume 1 minute. Assign […]
Greedy Algorithm for Egyptian Fraction
In early Egypt, people used to use only unit fraction (in the form of (1/n)) to represent the decimal numbers. The fraction was always written in the form 1/n , where the numerator is always 1 and denominator is a positive number. All other fractions were represented as the summation of the unit fractions. For […]
Greedy Solution to Activity Selection Problem.
Given n activities with their start and finish times. Select the maximum number of activities that can be performed by a single person, assuming that a person can only work on a single activity at a time. You cannot perform the activity before it starts. The start and end times for each activity are fixed. […]
Job Sequencing with given deadline
Given n Jobs, with their deadline (between 1 to n) and associated profit. Each job takes unit time to complete and you will get the profit only (when the job is completed before the deadline. Sequence the jobs in a way that the profit is maximized.
Max Distance between two occurrences of the same element
Given an array of integers where elements may be repeating in the array. Find the maximum distance between two occurrences of the same element in the array.
Swapping two variables without using third variable
This is a very common interview question, esp. at the junior level. Let us first write a function that swap two integers using a third variable.
Count max points on a line
Given n points on a 2D plane with (x, y) co-ordinates. Find the maximum number of points lie on the same straight line.
Print all Subsequences of an Array
Given an array of integers, Print all possible subsequences of that array.