Practice Problems Code
Practice Problems Code
Eggs
The following is a description of the instance of this famous puzzle involving N = 2 eggs and a building with K
= 36 floors.
Suppose that we wish to know which stories in a 36-story building are safe to drop eggs from, and which
will cause the eggs to break on landing. We make a few assumptions:
An egg that survives a fall can be used again.
A broken egg must be discarded.
The effect of a fall is the same for all eggs.
If an egg breaks when dropped, then it would break if dropped from a higher floor.
If an egg survives a fall then it would survive a shorter fall.
It is not ruled out that the first-floor windows break eggs, nor is it ruled out that the 36th-floor does
not cause an egg to break.
If only one egg is available and we wish to be sure of obtaining the right result, the experiment can be
carried out in only one way. Drop the egg from the first-floor window; if it survives, drop it from the second-
floor window. Continue upward until it breaks. In the worst case, this method may require 36 droppings.
Suppose 2 eggs are available. What is the least number of egg droppings that are guaranteed to work in all
cases?
The problem is not actually to find the critical floor, but merely to decide floors from which eggs should be
dropped so that the total number of trials is minimized.
Fence-painting
Given a fence with n posts and k colors, find out the number of ways of painting the fence such that at most
2 adjacent posts have the same color.
LIS
Given an array arr[] of size N, the task is to find the length of the Longest Increasing Subsequence (LIS) i.e.,
the longest possible subsequence in which the elements of the subsequence are sorted in increasing order.
Maze
A Maze is given as N*N binary matrix of blocks where source block is the upper left most block i.e., maze[0]
[0] and destination block is lower rightmost block i.e., maze[N-1][N-1]. A rat starts from source and has to
reach the destination. The rat can move only in two directions: forward and down.
In the maze matrix, 0 means the block is a dead end and 1 means the block can be used in the path from
source to destination. Note that this is a simple version of the typical Maze problem. For example, a more
complex version can be that the rat can move in 4 directions and a more complex version can be with a
limited number of moves.
Queens
The N Queen is the problem of placing N chess queens on an N×N chessboard so that no two queens attack
each other.
Valid parenthesis
An expression will be given which can contain open and close parentheses and optionally some characters,
No other operator will be there in string. We need to remove minimum number of parentheses to make the
input string valid. If more than one valid output are possible removing same number of parentheses then
print all such output.
E.g., ((())) is ok, (())) is not
Pathing
Given a directed graph, a source vertex ‘s’ and a destination vertex ‘d’, print all paths from given ‘s’ to ‘d’.
From <https://www.geeksforgeeks.org/assign-mice-holes/>
Policemen
Given an array of size n that has the following specifications:
1. Each element in the array contains either a policeman or a thief.
2. Each policeman can catch only one thief.
3. A policeman cannot catch a thief who is more than K units away from the policeman.
We need to find the maximum number of thieves that can be caught.
Examples:
Input : arr[] = {'P', 'T', 'T', 'P', 'T'},
k = 1.
Output : 2.
Here maximum 2 thieves can be caught, first
policeman catches first thief and second
police-
man can catch either second or third thief.
Input : arr[] = {'T', 'T', 'P', 'P', 'T', 'P'},
k = 2.
Output : 3.
Input : arr[] = {'P', 'T', 'P', 'T', 'T', 'P'},
k = 3.
Output : 3.
Shelving
Given length of wall w and shelves of two lengths m and n, find the number of each type of shelf to be used
and the remaining empty space in the optimal solution so that the empty space is minimum. The larger of
the two shelves is cheaper so it is preferred. However cost is secondary and first priority is to minimize
empty space on wall.