Sie sind auf Seite 1von 3

# PRAYATNA 2019

## MADRAS INSTITUTE OF TECHNOLOGY

OSPC (ONSITE PROGRAMMING CONTEST)
Time Duration: 45 minutes

## (i) Participants should not write anything in the question paper.

(ii) In your answer paper, each member must stick their QR sticker and write their unique participant ID below.
(iii) For coding questions, write only the necessary functions.

## Part A (2 Marks per question):

1) Swap two numbers without using a temporary variable.
2) Given the head pointer to a linked list, write a function to reverse it in place.
3) There are N boxes. They are stacked into rows such that the ith row contains i boxes. Write a function to find
the height of the stack so formed, given N. (Some boxes may be left unused)
Input: N = 7
Output: 3
4) Given N-2 integers, each in the range 1 to N (both inclusive) with no two integers being the same, find the
missing numbers.
Input
N=5
134
Output
25
5) You are given the endpoints of two lines. Write a function to find out if they intersect.
Input
L1: (2,3) (4,3)
L2: (3,4) (3,2)
Output
Yes
6) Draw the binary tree corresponding to the given in-order and post-order traversals
In-order: 6 2 8 7 1 3 5 9 4
Post-order: 6 8 7 2 9 5 4 3 1

7) You are given the initial positions in the plane and the constant velocity vectors of two points. Write a
function to find the instant of time at which they meet - if they do - or return –1 if they don’t.
Input:
Position1 (1,1)
Velocity1 (1,2)
Position2 (2,3)
Velocity2 (-1,-2)
Output:
0.5
8) Write a function to find out if the given number is prime.
9) Consider an excel sheet filled in row major order with integers starting from 1. Given an integer, print it's
row number and column name (assume there are 256 cols in a row and they are named A, B...Z, AA,AB,AC...IV)
Input: 256
Output: 1 IV
10) Write a function that takes a binary tree’s root node as input and returns true if the tree is a chain of
nodes and false if it is not.
For the following tree, the output is false
1
\
2
/ \
3 4
Output is true for this tree
1
/
2
\
3
11) Given a number N, find the number of perfect squares smaller than or equal to that number.
12) Given an array with elements decreasing up to a point and then increasing, find the minimum value. (All
elements in array are distinct)
13) Write a function to remove the least significant set bit from a number.
Input: 15
Output: 14

## Part B (5 marks per question):

1) Write a function to find the Nth fibonacci number (1 <= N <= 109)
Input: 5
Output: 5
2) Given a 2D matrix with each cell containing an integer i (i = 0 indicates water, i = any other integer indicates
that the cell belongs to person i), find the coordinates of the bottom left and top right corners of the smallest
rectangle that covers all cells owned by a person. Output the coordinates (rowIndex, colIndex) for each person
Input:
4 rows, 5 cols
10020
11022
01022
01120

Output:
Person 1: (4,1) (1,3)
Person 2: (4,4) (1,5)
3) Given an array, count the number of inversions in the array. An inversion is an unordered pair of indices in
the array (i,j) such that i < j and a[i] > a[j]
Input:
5
54321
Output:
10

4) Given an integer N, place N chess queens on an N x N board in such a way that no two queens threaten
each other. A queen can move any number of cells in all eight directions.
Input:
4
Output:
0100
0001
1000
0010
Here, a 1 indicates that the cell is occupied by a queen and 0 indicates an empty cell.

## Part C (8 Marks per question):

1) Given an array with N elements and a number S, find out the number of subsets in the array with sum equal
to S (N <= 1000, S <= 1000)
Input:
N = 3, S= 3
123
Output:
2
There are two subsets, {1,2} and {3} with sum equal to 3

2) You are given an array of length N (1 <= N <= 10e5). This is followed by Q queries, each of which can be of
the following two types:
Type 1: i x (change element a[i] to x)
Type 2: L R (find the xor of all elements in the range [L...R])

3) You are given a graph with N nodes. Initially, no two nodes are connected. This is followed by Q queries.
Each query can be of the following two types:
Type 1: u v (add an undirected edge between nodes u and v)
Type 2: Find the number of nodes in the largest connected component