0% found this document useful (0 votes)
177 views

Codathon Questions

Some coding questions to charge your brain

Uploaded by

Sahil Pahuja
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
177 views

Codathon Questions

Some coding questions to charge your brain

Uploaded by

Sahil Pahuja
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Problem 1 - Easy

Title: Snakes and Ladders

Problem Statement :

Sahil takes out his Snakes and Ladders game, stares at the board and wonders: "If I can always
roll the die to whatever number I want, what would be the least number of rolls to reach the
destination?"

Rules The game is played with a cubic die of 6 faces numbered 1 to 6.

1. Starting from square 1 , land on square 100 with the exact roll of the die. If moving the
number rolled would place the player beyond square 100, no move is made.
2. If a player lands at the base of a ladder, the player must climb the ladder. Ladders go up
only.
3. If a player lands at the mouth of a snake, the player must go down the snake and come
out through the tail. Snakes go down only.

Your Task is to find the minimum number of moves required to reach at 100.

Input :
● ladders: a 2D integer array where each ladder[i] contains the start and end cell numbers
of a ladder
● snakes: a 2D integer array where each snakes[i] contains the start and end cell numbers
of a snake

The first line contains the number of tests t,

For each test case:

- The first line contains n, the number of ladders.

- Each of the next n lines contains two space-separated integers, the start and end of a ladder.

- The next line contains the integer m, the number of snakes.

- Each of the next m lines contains two space-separated integers, the start and end of a snake.
Output :
For each of the t test cases, print the least number of rolls to move from start to finish on a
separate line. If there is no solution, print -1.

Sample Input:
2
3
32 62
42 68
12 98
7
95 13
97 25
93 37
79 27
75 19
49 47
67 17
4
8 52
6 80
26 42
2 72
9
51 19
39 11
37 29
81 3
59 5
79 23
53 7
43 33
77 21
Sample Output:
3
5

Problem 2 - Medium

Title: Almost Sorted Encyclopedia

Problem Statement :

Pradyumn loves to keep his encyclopedia sorted by numbers. But currently, he does not have
enough time to sort his encyclopedia. So he decided to keep it Almost sorted in intervals. The
Encyclopedia would be almost sorted if there is a consecutive subsequence in a sequence that
satisfies the following property:

1. The first number is the smallest.


2. The last number is the largest.

Please help him count the number of almost sorted encyclopedias in this permutation.

Note: Two intervals are different if at least one of the starting or ending indices are different.
Input :
The input consists of 2 lines. The first line contains an integer N while the second line contains a
permutation from 1 to N.

Output :
Output the number of almost sorted intervals

Sample Input:
5
4 1 2 5 3

Sample Output:
8

Explanation:
[4] , [1] , [1,2] , [1,2,5] , [2] , [2,5] , [5] , [3] are possible sorted combinations of encyclopedia

Problem 3 - Hard

Title: Sensor Network

Problem Statement:

A wireless sensor network consists of autonomous sensors scattered in an environment where


they monitor conditions such as temperature, sound, and pressure. Tanushree is a researcher
working on the Amazon Carbon-dioxide Measurement project. In this project, a wireless sensor
network in the Amazon rainforest gathers environmental information. The Amazon rainforest
stores an amount of carbon equivalent to a decade of global fossil fuel emissions, and it plays a
crucial role in the world’s oxygen-transfer processes. Because of the huge size of this forest,
changes in the forest affect not only the local environment but also the global climate by altering
wind and ocean current patterns. The goal of the project is to help scientists better understand
the earth’s complex ecosystems and the impact of human activities. Tanushree has an
important hypothesis and to test her hypothesis, she needs to find a subset of sensors in which
each pair of sensors can communicate directly with each other. A sensor can communicate
directly with any other sensor having distance at most d from it. In order for her experiments to
be as accurate as possible, Tanushree wants to choose as many sensors as possible.As one
does not simply walk into the Amazon, Tanushree cannot add new sensors or move those that
are currently in place. So given the current locations of the sensors, she needs your help to find
the largest subset satisfying her criteria. For simplicity, represent the location of each sensor as
a point in a two-dimensional plane with the distance between two points being the usual
Euclidean distance.

Input :
The input consists of a single test case. The first line contains two integers n and d (1≤n≤100

and 1≤d≤10 000), where n is the number of sensors available and d is the maximum distance

between sensors that can communicate directly. Sensors are numbered 1 to n. Each of the next

n lines contains two integers x and y (−10 000≤x, y≤10 000) indicating the sensor coordinates,

starting with the first sensor.

Output :
Display a maximum subset of sensors in which each pair of sensors can communicate directly.
The first line of output should be the size of the subset. The second line of output should be the
(one-based) indices of the sensors in the subset. If there are multiple such subsets, any one of
them will be accepted.

Sample Input:
5 20
0 0
0 2
100 100
100 110
100 120

Sample Output:
3
4 3 5

Problem 4 - Hard

Title: Wires

Problem Statement:

Moore’s Law states that the number of transistors on a chip will double every two years.
Amazingly, this law has held true for over half a century. Whenever current technology no
longer allowed more growth, researchers have come up with new manufacturing technologies to
pack circuits even denser. In the near future, this might mean that chips are constructed in three
dimensions instead of two.
But for this problem, two dimensions will be enough. A problem common to all two-dimensional
hardware designs (for example chips, graphics cards, motherboards, and so on) is wire
placement. Whenever wires are routed on the hardware, it is problematic if they have to cross
each other. When a crossing occurs special gadgets have to be used to allow two electrical
wires to pass over each other, and this makes manufacturing more expensive.
Our problem is the following: you are given a hardware design with several wires already in
place (all of them straight line segments). You are also given the start and endpoints for a new
wire connection to be added. You will have to determine the minimum number of existing wires
that have to be crossed in order to connect the start and endpoints. This connection need not be
a straight line. The only requirement is that it cannot cross at a point where two or more wires
already meet or intersect.
The figure shows the first sample input. Eight existing wires form 4 blocks. The start and
endpoints of the new connection are in the leftmost and rightmost squares, respectively. The
purple dashed line shows that a direct connection would cross three wires, whereas the optimal
solution crosses only two wires (the curved blue line).

Input:
The input consists of a single test case. The first line contains five integers m, x0, y0, x1, y1,

which are the number of pre-existing wires (m≤100) and the start and endpoints that need to be

connected. This is followed by m lines, each containing four integers xa, ya, xb, yb describing
an existing wire of non-zero length from (xa, ya) to (xb, yb). The absolute value of each input
coordinate is less than 105. Each pair of wires has at most one point in common, that is, wires
do not overlap. The start and endpoints for the new wire do not lie on a pre-existing wire.

Output:
Display the minimum number of wires that have to be crossed to connect the start and
endpoints.

Sample Input:
8 3 3 19 3
0 1 22 1
0 5 22 5
1 0 1 6
5 0 5 6
9 0 9 6
13 0 13 6
17 0 17 6
21 0 21 6

Sample Output:
2

Problem 5 - Medium

Title: Pizza Mania.

Problem Statement.

Ravi owns a pizza shop. A pizza at Ravi’s costs only ₹5. Ravi also has some policies
which are always withheld:

1. Each customer can buy exactly one pizza.


2. Ravi m only accepts payments in coins. There are only three types of coins: ₹5,
₹10, and ₹15.
3. It is only possible for someone to buy a pizza when Ravi can give them back
their change exactly ― for example, if someone pays with a ₹10 coin, Ravi needs
to have a ₹5 coin that he gives to this person as change.

There are N people (numbered 1 through N) standing in a queue to buy pizza. For
each i, the i- th person has one coin with value a[i]. Initially, Ravi has zero coins. Your
task is to find if Ravi can serve all the people in the queue or not.
Input:

The first line of the input contains a single integer T denoting the number of test cases. The
description of T test cases follows.

The first line of each test case contains a single integer N.


The second line contains N space-separated integers a1,a2,…,aN.

Output:

For each test case print Yes if all people can be served else print No.

Problem 6 - Medium

Title: Real Estate Expert

Problem Statement.
There are N houses for sale. The i-th house costs Ai dollars to buy. You have a
budget of B
dollars to spend.
What is the maximum number of houses you can buy?

Input:

The first line of the input contains a single integer T denoting the number of test cases. The
description of T test cases follows.
The first line of each test case contains two space-separated integers N and B.
The second line of each test case contains N space-separated integers in which the i-th
integer denotes the cost of i-th house (Ai).
Output:

For each test case, output a single integer S. Where S is the maximum number of houses
you can buy.

You might also like