An array of elements is given arr

arr is of length n

Right rotate array by k elements

Time complexity O(n) and space complexity O(1)

Sample Input:

arr = {1,2 ,3,4,5}

n = 5

k = 2

Output :

arr = {4,5,1,2,3}

A site for technical interview questions, brain teasers, puzzles, quizzles (whatever the heck those are) and other things that make you think!

An array of elements is given arr

arr is of length n

Right rotate array by k elements

Time complexity O(n) and space complexity O(1)

Sample Input:

arr = {1,2 ,3,4,5}

n = 5

k = 2

Output :

arr = {4,5,1,2,3}

It is raining at midnight – will we have sunny weather in 72 hours?

A half is a third of it. What is it?

When was the last year that looked the same upside down?

(1961)

A book costs $1 plus half its price. How much does it cost?

Question: You have two identical eggs. Standing in front of a 100 floor building, you wonder what is the maximum number of floors from which the egg can be dropped without breaking it. What is the minimum number of tries needed to find out the solution?

Suppose you had a Stack class. Write a new class MaxStack which, in addition to push() and pop(), has a method getMax() which returns the largest item in the stack. Use your existing Stack class to store the stack’s contents.

Don’t just use pop() to “dig” through your stack to find the max—do something that lets you return the max in constant time.

Solution

We could have an instance variable where we hold the max, but there’s a problem—when we pop that item from our stack it’s no longer the max. Now we have to “dig” through our stack to find the new max. Ideally we’d keep track of the current max as well as what the new max will be when that max is popped.

The trick is to have two instances of Stack inside our MaxStack. One holds the actual stack contents, while the other (call it maxesStack) holds the maxes. Whenever we push() an item, if it’s larger than the top item in maxesStack, we also push it to maxesStack. Whenever we pop() an item, if it’s the same as the top item in maxesStack(), we also pop() it from maxesStack.

So at any given point we can get the overall max in constant time be peeking at the top item in maxesStack.

How can you get a fair coin toss if someone hands you a coin that is weighted to come up heads more often than tails?

“How many unique areas of human knowledge have the right size of passionate users to make it as a Stack Exchange site?”

What is the most efficient way, memory-wise, to store 1 million phone numbers? Apparently this is an interview question at Google, although this seems like its a bit too easy.

Iconic One Theme | Powered by Wordpress