Sei sulla pagina 1di 20

EBOX PROGRAMMING QUESTIONS ­­­ Sample QB

Level

Objectives

1 Applying conditional statements to solve problems which need logical and reasoning skills.

2 Applying looping statements to solve problems which need logical and reasoning skills.

3 Problems which involves processing of collection of items (Arrays, Structures) and program modularity.

4 Problems which involves application and implementation of appropriate data structures.

5 Problems which involve Strategic Modeling and Algorithmic Thinking.

SAMPLE QUESTIONS

LEVEL 1

Vehicle Parking

Sita reaches Fun Mall in her Audi and she gets down in the entrance and she asks the driver to park the car in the 1 st freely available slot in the mall's parking lot (if it is available) or in the ground adjacent to the mall.

The driver asks her to give a call when she is done with her shopping and leaves to park the car.

Suddenly Sita remembers that she has left her mobile in home. She sees a display board in the Mall entrance that gives the status of the occupancy of the parking lot.

There are 4 basements ­­­ B1, B2, B3 and B4. Assume that only when B1 is full, cars would be allowed to be parked in B2 and so on. The number of cars that can be parked in the basements B1, B2, B3 and B4 are N1, N2, N3 and N4. The total number of cars parked in the parking lot currently is N. The values of N1, N2, N3, N4 and N are displayed in the giant display board in the mall entrance.

Help Sita in quickly deciding where Raju would have parked the car.

[Assume that no other car is in queue to get parked and no other car leaves at this time].

Input and Output Format:

Input conists of 5 integers corresponding to N1, N2, N3, N4 and N. Output consists of a string which is either 'B1' or 'B2' or 'B3' or 'B4' or 'EXT'. EXT corresponds to external parking in the adjacent ground.

Sample Input 1:

50

50

40

40

120

Sample Output 1:

B3

Sample Input 2:

50

50

40

40

180

Sample Output 2:

EXT

Circle Intersection

In this problem you must write a program that determines if two circles intersect each other. The input to your program will be the <x, y> coordinates for the center of each circle, followed by the radius(r) of each circle. The output will state whether the circles overlap, do not overlap, or are tangential (i.e., tangential circles touch each other at just one common point).

Input Format:

Input consists of 6 integers. The first integer corresponds to the x­coordinate of the centre of the first circle. The second integer ccorresponds to the y­coordinate of the centre of the first circle. The third integer corresponds to the radius of the first circle. The next 3 integers correspond to the x,y and radius of the second circle.

Output Format:

The output consists of a single line which contains any of these 3 strings ­­­ “The circles are tangential”, “The circles overlap”, “The circles do not overlap”

Sample Input 1:

10

10

3

10

1

Sample Output 1:

The circles are tangential

Sample Input 2:

8

8

3

8

4

2

Sample Output 2:

The circles overlap

FEE COLLECTION

Write a program to determine the fee amount to be collected from a student. The input to the program are the type of the student, tuition fee, bus fee, hostel fee. Refer the table below for fee details.

Student Type

Student Type denoted as

Fee Details

Merit Seat Day Scholar

MSDS

Tuition fee + Bus fee

Merit Seat Hosteller

MSH

Tuition fee + Hostel fee

Management Seat Day Scholar

MGSDS

150% of Tuition fee + Bus fee

Management Seat Hosteller

MGSH

150% of Tuition fee + Hostel fee

Input and Output Format:

Input consists of a string (student type), tuition fee(float), bus fee(float) and hostel fee(float). All floating point numbers are displayed correct to 2 decimal places. Refer sample input and output for formatting specifications.

All text in bold corresponds to input and the rest corresponds to output.

Sample Input and Output :

Enter the student type MSH Enter tuition fee

40000

Enter bus fee

12000

Enter hostel fee

50000

The fees to be paid by the student is Rs.90000

PROFIT OR LOSS

A fruitseller buys a dozen mangoes at Rs.X. He sells 1 mango at Rs.Y. Write a program to determine the profit or loss in Rs. for the fruitseller.

Input and Output Format:

Input consists of 2 floating point numbers which correspond to X and Y. Refer sample input and output for formatting specifications. All floating point numbers are displayed correct to 2 decimal places. All text in bold corresponds to input and the rest corresponds to output.

Sample Input and Output 1:

Enter the price of a dozen mangoes

60.0

Enter the price at which 1 mango is being sold

4

Loss : Rs.12.00

Sample Input and Output 2:

Enter the price of a dozen mangoes

60.0

Enter the price at which 1 mango is being sold

6

Profit : Rs.12.00

Sample Input and Output 3:

Enter the price of a dozen mangoes

60.0

Enter the price at which 1 mango is being sold

5

No profit nor loss

Isosceles Triangles Let (x1, y1),(x2, y2), and (x3, y3) be three points in the plane. These points are colinear if

(x1 x2)(y2 y3) = (y1 y2)(x2 x3). They form the vertices of a triangle if they are not colinear. This triangle is isosceles if at least two of its edges have the same length. Write a program that takes six integers defining three points and reports whether these three points form an isosceles triangle.

Input Format:

Input consists of 6 integers. The 6 integers correspond to x1,y1,x2,y2,x3 and y3 in the same order.

Output Format:

Output consists of a single string (“yes” if it is an isoceles triangle or “no” if it is not an isoceles triangle).

Sample Input 1:

1

2

3

4

5

6

Sample Output 1:

no

Sample Input 2:

0

0

1

4

2

0

Sample Output 2:

yes

LEVEL 2

Character Pattern

Write a program to generate the following pattern.

**********

****bb****

***bbbb***

**bbbbbb**

*bbbbbbbb*

**bbbbbb**

***bbbb***

****bb****

**********

Input and Output Format:

Input consists of a single integer that corresponds to n, the number of stars in the 1 st and last row. N is always an even number

Sample Input :

10

Sample Output :

**********

****bb****

***bbbb***

**bbbbbb**

*bbbbbbbb*

**bbbbbb**

***bbbb***

****bb****

**********

Series

The first few terms in the series are ­­ 2,5,9, ,20,27

Write a program to generate the first n terms in this series

Input Format:

Input consists of a single integer which corresponds to n. Output Format:

Output consists of the terms in the series separated by a blank space.

Sample Input:

6

Sample Output:

2 5 9 14 20 27

AMOEBA MULTIPLICATION

The environmental eco club have discovered a new Amoeba that grows in the order of a Fibonacci series every month. They are exhibiting their amoeba in a national conference. They want to know the size of the amoeba at a particular time instant. If a particular month’s index is given can you write a program that displays the amoeba’s size……???

Note: The size of the amoeba on month 1, 2, 3, 4, 5, 6,

will be 0, 1, 1, 2, 3, 5, 8 respectively.

Sample input and Output 1:

[All text in bold corresponds to input and the rest to output] Enter the number of Months :

7

The amoeba size is 8

Sample input and Output 2:

Enter the number of Months :

12

The amoeba size is 89

Cryptography

Cryptography is about protecting information. Early schemes involved switching letters.

A common approach was using an encoding wheel that “moved” every letter the same number of

letters down in the alphabet. The letters on the wheel “wrap around” (a follows z). Write a program that accepts an integer n and a word less than 10 letters long. Print out the encoded word with every letter replaced by the nth successor.

Input Format:

Input consists of 2 lines. The first line of the input is an integer which corresponds to n. The second line of the input is the string.

Output Format:

Output consists of a string.

Sample Input 1:

1

example

Sample Output 1:

fybnqmf

Sample Input 2:

25

peanut

Sample Output 2:

odzmts

Digital Roots

We define the digital root of a nonnegative integer n as follows. Let m be the sum of the digits in n.

If m is a one­digit number, then m is the digital root of n. Otherwise, the digital root of n is equal to

the digital root of m.

Develop a program that, given as input a nonnegative integer, outputs its digital root.

Input Format:

Input consists of a single integer.

Output Format:

Refer sample output.

Sample Input 1:

7

Sample Output 1:

7

Sample Input 2:

3452

Sample Output 2:

5

LEVEL III

Zero Duplicates

Write a program that will read in a list of numbers and will then print out the same list except numbers that have already been printed will be printed as a zero instead.

Input Format:

Input consists of n+1 integers where n is the number of elements in the list. The first integer corresponds to n. The remaining n integers correspond to the elements in the list.

Assume that the maximum value of n is 100.

Output Format:

Output consists of n integres on separate lines.

Sample Input :

5

2

3

4

3

Sample Output:

2

3

4

0

6

Encryption A secret code reorders the letters in a message using an array. E.g. the following message "Attack at noon or we are done for" is placed in a 6*6 array :

Attack

*at*no

on*or*

we*are

*done*

for

Blank letters are replaced by asterisks, and full stops are added to the message so that it fills out the array. The coded message can then be read down the columns, i.e.

A*ow*ftanedott**ora*oan.cnrre.ko*e*.

Write a program that will encrypt a message as above. Use an array with 6 columns, and adjust the number of rows depending on the message length.

Input Format:

Input consists of a string with a maximum of 100 characters.

Output Format:

Output consists of a string which corresponds to the encrypted message.

Sample Input:

Attack at noon or we are done for

Sample Output:

Attack

*at*no

on*or*

we*are

*done*

for

SEATING ARRANGEMENT

The seating arrangement plan for the 1 st internals was missing on the day of the exam. The exam cell coordinator was very tensed and he requested your help in quickly generating the seating arrangement order.

The only constraint is that no two students of the same department should sit next to each other (both rowwise and columnwise). He decided that he will just generate a random list and check whether the given constraint is satisfied.

Assume that 5 students can be seated in a single row. ECE department is represented by code EC, CSE department is represented by code CS, Mechanical department is represented by code ME and Information Technology department is represented by code IT.

Given the seating plan for n students, determine whether it is a valid arrangement or not.

Input Format:

The first line of the input consists of an integer which corresponds to the number of students. The next n lines of the input consists of a string which corresponds to the department code.

Output Format:

Output consists of a single string (“Valid” or “Invalid”).

Sample Input 1:

8

CS

EC

ME

IT

CS

EC

CS

ME

Sample Output 1:

Invalid

Sample Input 2:

8

CS

EC

ME

IT

CS

CS

EC

ME

Sample Output 2:

Invalid

Sample Input 3:

CS

EC

ME

CS

EC

IT

ME

EC

Sample Output 3:

Valid

Conflicting Lists

Our good friend, the king, has the habit of honouring some of the officers from his Army on the

anniversary of his coronation. The king has two Chief Ministers and each of them insists on having a say in deciding the list of officers to be honoured. The king asks each minister to rank the officers

in the order of merit. Of course, the ministers are quite unlikely to order the officers identically.

So, how does the King select the honours list? He realises that if some J officers were chosen and they don't happen to be the first J officers in a minister's list, then he would know that his opinion has been superseded and that would make him quite unhappy, and unhappy ministers can make the king quite unhappy!

Honouring a officer involves giving cash awards, promotions and so on, all of which costs money and the King would like to minimise his expenditure. So, he realises that the idea is to find the

smallest set of officers such that, if the set has J elements, then the first J elements in both the lists consist of these J officers, though the ordering may be different in the two lists.

For example, if there are 10 officers, numbered 1,2, ministers orders are:

,10,

and the lists handed out by the two

3

5 7 1 2 4 6 8 9 10

and

1

2 3 4 7 5 6 8 9 10

where the lists are in decreasing order of merit. If the king choses to honour the officers in the set {1,2,3,4,5,7} then both ministers would believe that he picked the first 6 officers from his list. You can check that no set of smaller size has this property. Your task is to help the king make his choice.

Input format

The first line of the input contains a single integer N, indicating the number of officers in the King's

army. We take the ministers to be {1,2,

the ranking of the officers, in decreasing order of merit, according to the two ministers.

,N}.

The next two lines each contain N integers, describing

Output format

A single integer giving the size of the smallest list of officers he can honour without disappointing

either of his ministers.

Test data You may assume that 1 N 100000. You may further assume that in at least 20% of the inputs 1 N 1000.

Example We now illustrate input and output formats using the example described above.

Sample input

10

3 5 7 1 2 4 6 8 9 10

1 2 3 4 7 5 6 8 9 10

Sample output

6

FOO FIGHTER

The Committee for Tidy Language has declared the string "FOO" to be untidy. They have therefore decided to replace it by "OOF". However,this can create new instances of "FOO". You are to write a program that goes through a string of characters as many times as necessary, replacing each instance of "FOO" by "OOF" until no more instances exist.

INPUT FORMAT:

A string of up to 256 uppercase letters.

OUTPUT FORMAT:

A string of up to 256 uppercase letters obtained by repeatedly replacing every occurence of "FOO" by "OOF".

SAMPLE INPUT

FOOOOOPLE

SAMPLE OUTPUT

OOOOFOPLE

LEVEL IV

Binary Search Tree

Implement a C program to construct a Binary Search Tree and also display the elements in the tree using Inorder , Preorder and Postorder traversals.

Binary Search tree is a binary tree in which each internal node x stores an element such that the element stored in the left subtree of x are less than or equal to x and elements stored in the right subtree of x are greater than or equal to x. This is called binary­search­tree property.

Input and Output Format:

Refer Sample Input and Output for formatting specifications.

[All text in bold corresponds to input and the rest corresponds to output] Sample Input and Output:

Enter the element to be inserted in the tree

1

Do you want to insert another element? yes

Enter the element to be inserted in the tree

2

Do you want to insert another element? yes

Enter the element to be inserted in the tree

3

Do you want to insert another element? yes

Enter the element to be inserted in the tree

4

Do you want to insert another element? no Inorder Traversal : The elements in the tree are 1 2 3 4 Preorder Traversal : The elements in the tree are 1 2 3 4 Postorder Traversal : The elements in the tree are 4 3 2 1

Linked List : Append

Write a program to append a node to the Linked List.

Define a structure struct node

{

int data;

struct node * link;

}

Include functions

append ­­­ to add data at the end of the linked list.

display ­­­ to display all the data in the linked list.

Refer function specifications for further details. [Note: The stmt. 'Elements in the linked list are' should be in the main function.

Input and Output Format:

Refer sample input and output for formatting specifications.

Sample Input and Output:

[All text in bold corresponds to input and the rest corresponds to output.]

Enter the value

10

Do you want to add another node? Type Yes/No Yes Enter the value

17

Do you want to add another node? Type Yes/No Yes Enter the value

11

Do you want to add another node? Type Yes/No Yes Enter the value

28

Do you want to add another node? Type Yes/No No The elements in the linked list are 10 17 11 28

Function Definitions:

void append(struct node **, int ) void display(struct node *)

Linked List : Delete any element

Write a program to delete a particular element in the Linked List. Refer function specifications for further details.

Input and Output Format:

Refer sample input and output for formatting specifications.

Sample Input and Output:

[All text in bold corresponds to input and the rest corresponds to output.] Enter the value

4

Do you want to add another node? Type Yes/No Yes Enter the value

1

Do you want to add another node? Type Yes/No Yes Enter the value

8

Do you want to add another node? Type Yes/No Yes

Enter the value

1

Do you want to add another node? Type Yes/No No The elements in the linked list are 4 1 8 1 Enter the element to be deleted

8

The elements in the linked list after deleting the element are 4 1 1

Function Definitions:

void append(struct node **, int) void display (struct node *) void delete (struct node **, int)

Stack : Array Implementation : Push and Pop

Consider implementing a dynamically sized stack using an array.

Create a structure struct stack

{

int * a; int top; int maxSize; };

The structure stack has a pointer 'a' to a dynamically allocated array (used to hold the contents of the stack), an integer 'maxSize' that holds the size of this array (i.e the maximum number of data that can be held in this array) and an integer 'top' which stores the array index of the top element in the stack.

Write a program to implement push and pop operation on stack and to display the contents of the stack.

In the initstack function intialize the value of top to ­1 and initialize the value of maxSize. Print the message “Stack is full” in the push function when an attempt is made to push a data into a full stack. Print the message “Stack is empty” in the pop function and return the value ­1000 when an attempt is made to pop data from an empty stack.

Refer function specifications for further details.

Input and Output Format:

Refer sample input and output for formatting specifications.

Note that the statement “The contents of the stack are” is in the main function. In the display function, if the stack is empty, print “ {}”.

[All text in bold corresponds to input and the rest corresponds to output] Sample Input and Output:

Enter the maximum size of the stack

3

Choice 1 : Push Choice 2 : Pop Choice 3 : Display Any other choice : Exit Enter your choice

1

Enter the element to be pushed

1

Choice 1 : Push Choice 2 : Pop Choice 3 : Display Any other choice : Exit Enter your choice

1

Enter the element to be pushed

2

Choice 1 : Push Choice 2 : Pop Choice 3 : Display Any other choice : Exit Enter your choice

1

Enter the element to be pushed

3

Choice 1 : Push Choice 2 : Pop Choice 3 : Display Any other choice : Exit Enter your choice

1

Enter the element to be pushed

4

Stack is full Choice 1 : Push Choice 2 : Pop Choice 3 : Display Any other choice : Exit Enter your choice

3

The contents of the stack are 1 2 3 Choice 1 : Push Choice 2 : Pop Choice 3 : Display

Any other choice : Exit Enter your choice

2

The popped element is 3 Choice 1 : Push Choice 2 : Pop Choice 3 : Display Any other choice : Exit Enter your choice

2

The popped element is 2 Choice 1 : Push Choice 2 : Pop Choice 3 : Display Any other choice : Exit Enter your choice

2

The popped element is 1 Choice 1 : Push Choice 2 : Pop Choice 3 : Display Any other choice : Exit Enter your choice

2

Stack is empty Choice 1 : Push Choice 2 : Pop Choice 3 : Display Any other choice : Exit Enter your choice

3

The contents of the stack are {} Choice 1 : Push Choice 2 : Pop Choice 3 : Display Any other choice : Exit Enter your choice

4

LEVEL V

ROBOT

Imagine a robot sitting on the upper left hand corner of an NxN grid. The robot can only move in two directions: right and down. The robot has to reach the lower right hand corner of the NxN grid. Write a program to determine the number of possible paths for the robot.

Input and Output Format:

Refer sample input and output for formatting specifications. All text in bold corresponds to input and the rest corresponds to output.

Sample Input and Output:

Enter the size of the grid

3

The number of paths is 6

Flood Fill Algorithm

Flood fill, also called seed fill, is an algorithm that determines the area connected to a given node in a multi­dimensional array. It is used in the "bucket" fill tool of paint programs to fill connected, similarly­colored areas with a different color, and in games such as Go and Minesweeper for determining which pieces are cleared.

Implement the “Flood fill” or “Paint fill” function that one might see on many image editing programs. That is, given a screen (represented by a 2 dimensional array of Colors) that has multiple objects and a new color, fill in the entire area of multiple objects with different colors. The first object is filled with the new color value, the second object is filled with new color value + 1 and so on.

//Use 4­connected neighbours(left, right, top, bottom) to fill the object. Input and Output Format:

Refer sample input and output for formatting specifications. All text in bold corresponds to input and the rest corresponds to output.

Sample Input and Output:

Enter the size of the N x N image:

5

Enter the pixel values in the image 0/1

0

0 1 1 1

0

0 0 1 1

1

1 0 0 0

1

1 1 0 0

1

0 0 0 0

Enter the color/value to be filled for the 1st object

4

Image before filling:

0

0

1

1 1

0

0 0 1 1

1

1 0 0 0

1

1 1 0 0

1

0 0 0 0

Image after filling:

0 0 4 4 4

5

5 0 0 0

5

5

5 5 0 0 0 0 0 0

Rotated Sorted Array

Given a sorted array of n integers that has been rotated an unknown number of times, design and implement an O(log n) algorithm that finds an element in the array. You may assume that the array was originally sorted in increasing order.

Input and Output Format:

Refer sample input and output for formatting specifications.

[All text in bold corresponds to input and the rest corresponds to output]

Sample Input and Output:

Enter the number of elements in the array

12

Enter the elements in the array

15

16

19

20

25

1

3

4

5

7

10

14

Enter the element to be searched

5

The index of element 5 in the array is 8

Parenthesis

Write a program to to print all valid (e.g., properly opened and closed) combinations of n­pairs of parentheses.

Input and Output Format:

Input consists of a single integer which corresponds to n. Refer sample input and output for formatting specifications. All text in bold corresponds to input and the rest corresponds to output.

Sample Input and Output:

Enter parenthesis count

3

Valid combinations of parenthesis are ((())) (()()) (())() ()(()) ()()()