# Coding Ninja Interview Experience

Objective

A train130 meters long travels at a speed of 45 km/hr crosses a bridge in 30 seconds. The length of the bridge is:

270 m

235 m

245 m

220 m

A total of 250 players participated in a single tennis knock out tournament. What is the total number of matches played in the tournament? (Knockout means if a player loses, he is out of the tournament). No match ends in a tie.

125

250

249

124

The Pokemon Republic has some very strange customs. Couples only wish to have female children as only females can inherit the family’s wealth, so if they have a male child they keep having more children until they have a girl. If they have a girl, they stop having children. What is the ratio of girls to boys in Pokemon Republic?

1:1

2:1

1:2

3:2

If the probability that Stock A will increase in value during the next month is 0.54, and the probability that Stock B will increase in value during the next month is 0.68. What is the greatest value for the probability that neither of these two events will occur?

0.22

0.32

0.37

0.46

There is a circular jail with 100 cells numbered from 1 to 100. One night the jailor gets drunk and starts running around the jail in circles. In his first round he opens every door. In his second round he visits every 2nd door (2,4,6,…) and shuts the door. In the 3rd round he visits every 3rd door (3,6,9,…) and if the door is shut he opens it, if it is open he shuts it. This process of crazy drunken-ness continues for 100 rounds and finally the jailor gets exhausted and falls down. Let us assume the prisoners are all kind enough to wait until the crazy jailor stops and finally they decide to escape. By the end of all this, how many prisoners find their doors open?

100

50

10

25

What is the output of following C program?

#include <stdio.h>

int main()
{
int *ptr, a = 10;
ptr = &a;
*ptr += 1;
printf(“%d,%d/n”, *ptr, a);
}

10,10

10,11

11,10

11,11

What is the output of following program?

#include <stdio.h>

void foo( int[] );
int main()
{
int ary = {1, 2, 3, 4};
foo(ary);
printf(“%d “, ary);
}
void foo(int p)
{
int i = 10;
p = &i;
printf(“%d “, p);
}

10 10

Undefined Behaviour

Compile Time Error

10 1

What is the time complexity of following code?

while(n){
j=n;
while(j>1){
j-=1;
}
n/=2;
}

O(n^2)

O(n)

O(nlogn)

None of These

What is the time complexity for the following function?

void fun2(int m, int n = 1)
{
if (n <= 0)
return;
if (n > m)
return;
fun2(m, 2*n);
}

O(logn)

O(logm)

O(m)

O(n)

Which of the following is FALSE about references in C++?

References cannot be NULL

A reference must be initialized when declared

Once a reference is created, it cannot be later made to reference another object; it cannot be reset.

References cannot refer to constant value

Programming

#### Without using any addition LL arrange elements in a LL such that all even numbers are placed after odd numbers, relative order of even and odd shouldn’t change .

Input Format : Input contains multiple numbers each with separated by a space except last ,representing each node of linked list with -1 stating as end or NULL.

Output Format: Print the output LL, each element should be separated by a space and LL end should be shown by a -1

Sample test case : 1 2 3 4 -1

Output: 1 3 2 4 -1

Function Signature: ListNode & Node classes are defined for you in the sample file.

CPP: ListNode* oddEvenList(ListNode* head);

Java: public static Node<Integer> evenAfterOdd(Node<Integer> head);

#### Find equilibrium index of an array. Equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to the sum of elements at higher indexes.

Input format: Input contains size of the array followed by the array

Output format: Print the obtained index

Sample Test:

Input:

7

-7 1 5 2 -4 3 0

Output:

3

(implies that a+a+a = a+a+a)

Function Signature:

CPP: int equilibrium_Index(vector<int> &ar); Java: private static int getEquilibriumIndex(int[] input);

#### Given a integer h, find the possible number of balanced binary trees of height h. This number can be huge, so return output modulus 10^9 + 7.

Sample Input: 3

Sample Output: 15

You need to write a function with following signature.

C++: int BTreeOfGivenHeight(int Required_height);

Java: public static int balancedTreesOfHeight(int height);

INTERVIEW

Technical

1. Given a string s, generate all the possible substrings of s. E.g. if s  = “abc” output should be {“”, a, b, c, ab, bc, abc}
2. Given a string s, find the length of maximum possible palindrome that can be generated from s.
3. Given a Binary Tree, Count the number of nodes.
4. Given a Binary tree, print all nodes that does not have their siblings.
5. Given a Binary Tree, print all leaf nodes.

Behavioral

1. Explain your best project.
2. What motivated you to join Coding Ninjas.
3. Where do you see yourself in five years.
Advertisements

This site uses Akismet to reduce spam. Learn how your comment data is processed.