Top companies like Google, Microsoft, frequent a lot of problems in interviews. The basic research will yield a lot of results, but we went a step further to pull out the most common problems. Knowledge of these problems will definitely give you an edge over your competitors in interviews.

### Profit Maximization :

**Input:**

First line contains number of test cases ‘T’. Each test case contains the integer value ‘N’, denoting the number of days followed by an array of oil barrel prices in N days.

**Output:**

The days on which the barrels should be bought and sold to maximize profit should be displayed as shown below. And if there is no possibility to make profit then print -1. Multiple deals have to be included in the same line. One cannot buy more than one barrels of oil at the same time. Meaning you have to sell the barrel before buying again.

**Example**

**Input:**

3

8

130 190 230 310 40 285 565 645

10

34 16 24 28 38 17 31 46 62 69

7

79 65 34 29 29 24 19

**Output:**

(0 3) (4 7)

(1 4) (5 9)

-1

### Word Reversal:

Given a string of length N reverse the order of words in it. Words are separated by dots.

**Input:**

The first line contains ‘T’, denoting the number of testcases. This is followed by individual test cases. Each case contains a string made up of spaces and characters.

**Output:**

For each test case, output a single line containing the string with the reversed words.

**Example:**

**Input:**

3

i.is.break.random.talk.lobo

ako.bobo

bad.like.alpha.cha lie

**Output:**

lobo.talk.random.break.is.i

bobo.ako

cha lie.alpha.like.bad

### Tree Balance:

Given a binary tree, find if it is height balanced or not. A tree is height balanced if the difference between heights of left and right subtrees is not more than one for all nodes of tree. Expected time complexity is O(n).

A height balanced tree

1

/ \

10 39

/

5

An unbalanced tree

1

/

10

/

5

**Input:**

The task is to complete the method which takes one argument, root of Binary Tree. The following characters use the mentioned format:

N M (L/R) , N mentions the root for the node M, L/R defines whether the element M is part of the left subtree or right subtree.

There are multiple test cases. For each test case, this method will be called.

**Output:**

The function should return true if tree is height balanced, else false.

**Example:**

**Input:**

2

2

1 2 L 2 3 L

4

10 20 L 10 30 R 20 40 L 20 60 R

**Output:**

False

True

### Number of Cities:

A group of connected 1s forms a City now your task is to complete the method findCity which returns the no of Cities present. The function takes three arguments. The first is the boolean matrix A and then the next two arguments are N and M denoting the size of the matrix A .

**Input:**

The first line of input will be the no of test cases T. Then T test cases follow. The first line of each test case contains two space separated integers N and M. Then in the next line are the NxM inputs of the matrix A separated by a space.

**Output:**

The expected output will be the total no of cities present.

**Example :**

**Input**

1

3 3

1 1 0 0 0 1 1 0 1

**Output**

2

There are a lot of other questions that are frequented by many companies. While there is no way to know which problems you will be asked, it is best to remain prepared. This is a small list of questions, but you can search for the most commonly asked questions to improve your prep.

We will be releasing the solution in a week’s time so that you can have time to work on the problem from your end.

For other more interesting and challenging problems, log on to Xobin(https://xobin.com ) and interact with the bot.

## 1 Comment

Excellent post!