## Bloomberg LP Interview Question for Financial Software Developers

• 0

Comment hidden because of low score. Click to expand.
2
of 2 vote

``````The ideal is to have two stack. One which will store elements and the other which will store min elements till now

10  10

20  10
10  10

30 10
20 10
10 10

5  5
30 10
20 10
10 10

3  3
5  5
30 10
20 10
10 10

Minimum Till now is top of 2nd Stack ie 3

Remove 3

5  5
30 10
20 10
10 10``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

O(n) shouldn't be a problem. can we get any better?

Comment hidden because of low score. Click to expand.
0
of 0 vote

T min(stack <T> s)
{

}

Comment hidden because of low score. Click to expand.
0
of 0 vote

T min(stack <T> s)
{
T t = T.Max();
stack <T> s1;
while(!s1.empty())
{
if (s1.top() < t)
t = s1.top();
s1.push(s1.top());
s.pop();
}
while(!s1.empty())
{
s.push(s1.top());
s1.pop();
}
return t;

}

Comment hidden because of low score. Click to expand.
0
of 0 vote

we can have each element in the stack to maintain a min Element below the stack.
Whenever a new element is added we check if the element is lesser than the Min below the Top of stack and if so we make the min Element below the new element being added as the element itself(As it is for the first element itself).

Comment hidden because of low score. Click to expand.
0

Tricky part,
What If Min() is called twice?

Comment hidden because of low score. Click to expand.
0
of 0 vote

pop all elements out and save them into an array.

Comment hidden because of low score. Click to expand.
0
of 0 vote

keep track of the min value within the current in stack in its topmost element.

Comment hidden because of low score. Click to expand.
0

What if I did pop() and then min ??

Our algo will produce erroneous output . Am I right ?

Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.ArrayList;
import java.util.Collections;
import java.util.Stack;

public class C {

public static void main(String[] args) {

Stack<Integer> st = new Stack<Integer>();
ArrayList<Integer> list = new ArrayList<Integer>();

st.push(12);
st.push(15);
st.push(6);
st.push(2);
st.push(23);
st.push(18);

while(!(st.isEmpty())){

int top = st.pop();

}

System.out.println(list);

Collections.sort(list);

System.out.println(list.get(0));
}

}

Comment hidden because of low score. Click to expand.
0
of 0 vote

Taking cue from the solution about maintaining a separate stack, below is an implementation:

class StackX{
private int stackSize;
private long[] stackArray;
private int top;

private long min;
private long[] orderedStackArray;

public StackX(int stackSize){
this.stackSize = stackSize;
stackArray = new long[stackSize];
top = -1;
orderedStackArray = new long[stackSize];
}

public void push(long item){
stackArray[++top] = item;

if(min == 0.0)
min = item;

if(item < min){
orderedStackArray[top] = item;
min = item;
} else {
orderedStackArray[top] = min;
}
}

public long pop(){
return stackArray[top--];
}

public long peek(){
return stackArray[top];
}

public long peekOrderedStack(){
return orderedStackArray[top];
}

public boolean isEmpty(){
return (top == -1);
}

public boolean isFull(){
return (top == stackSize-1);
}

public long min(){
return orderedStackArray[top];
}

}

class StackApp{

public static void main(String[] args){
StackX stackX = new StackX(10);
stackX.push(40);
stackX.push(20);
stackX.push(5);
stackX.push(60);

System.out.println(stackX.min());

}
}

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.