## Microsoft Interview Question for Interns

Country: United States

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

Here it is : [ stackoverflow.com/questions/26974160/sorting-a-stack-using-another-stack ]

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

``````function length (A) {
return A.length
}

function peek (A) {
return A[length(A) - 1]
}

function push (A, x) {
return A.push(x)
}

function pop (A) {
return A.pop()
}
module.exports = function (A) {
var B = []
if (length(A) === 1) {
// A is trivially sorted...
return A
}
push(B, pop(A))
// B is trivially sorted in descending order
while (length(A)) {
var a = pop(A)
var b = peek(B)
var i = 0
while (b < a) {
push(A, pop(B))
b = peek(B)
++i
}
push(B, a)
for (; i > 0; --i) {
push(B, pop(A))
}
}
// A is empty, B is in descending order
while (length(B)) {
push(A, pop(B))
}
return A
}

var A = module.exports([4, 7, 2, 8, 1, 6, 5])
console.log(A)``````

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

Ruby

``````def stack_sort stack1
stack2 = []
held = nil
until stack1.empty?
until  stack1.length == 1 || stack1[-2] < stack1[-1]
stack2 << stack1.pop
end
stack2 << stack1.pop
held = stack1.pop
until  stack2.empty? || held.nil? || stack2[-1] < held
stack1 << stack2.pop
end
stack2 << held unless held.nil?
end
[stack1, stack2]
end``````

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

Ruby

``````def stack_sort stack1
stack2 = []
held = nil
until stack1.empty?
until  stack1.length == 1 || stack1[-2] < stack1[-1]
stack2 << stack1.pop
end
stack2 << stack1.pop
held = stack1.pop
until  stack2.empty? || held.nil? || stack2[-1] < held
stack1 << stack2.pop
end
stack2 << held unless held.nil?
end
[stack1, stack2]
end``````

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

[knowsh.com/Notes/NotesSearch/NotesDetail/160269/Program-To-Sort-A-Stack-Using-Only-One-Other-Stack-And-Without-Recursion]

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

find the solution on knowsh-dot-com

/Notes/NotesSearch/NotesDetail/160269/Program-To-Sort-A-Stack-Using-Only-One-Other-Stack-And-Without-Recursion

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

find the solution on knowsh com

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

``````def sort_stack(st):
res_st = []
while len(st):
elt = st.pop()
while len(res_st) and res_st[-1] < elt:
st.append(res_st.pop())

res_st.append(elt)

return res_st``````

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

``````void sort( std::stack<int>& s)
{
std::stack<int> temp;

while( !s.empty() )
{
int i =  s.top(); s.pop();
while ( !temp.empty() && ( ( !s.empty() ) && (i > s.top() ) ) ){
temp.push(s.top() ); s.pop();
}
temp.push(i);

}
swap(s,temp);
}``````

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

``````void SortStack(Stack s1)
{
Stack s2 = new Stack();
while(s1.Count > 0)
{
int next = s1.Pop();
while(s2.Count > 0)
{
if(next > s2.Peek())
{
s2.Push(next);
break;
}
else
{
s1.Push(s2.Pop());
}
s2.Push(next);
}
while(s2.Count > 0)
s1.Push(s2.Pop());
}

}``````

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

``````public static Stack<Integer> stackSort(Stack<Integer> s1) {
Stack<Integer> s2 = new Stack<Integer>();
while (s1.size() > 0) {
int next = s1.pop();
while (s2.size() > 0) {
int sorted = s2.pop();
if (sorted < next)
s1.push(sorted);
}
s2.push(next);
}
return s1;
}``````

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.