Microsoft Interview Question for SDE-2s


Country: United States
Interview Type: In-Person




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

Object remove(Stack<Object> s){

if(s.size()==1){
return s.pop();
}

Object ele=s.pop();
Object res=remove(s);
s.push(ele)
return res;
}

- Mani K December 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

In C++ this can be done like this :

void PrintQ( MyStack<int> S ){
			if(S.empty()){
				return;
			}
			int temp = S.pop();
			PrintQ(S);
			cout<<temp<<"\n";
		}

Do let me know if its not the case.

Although subtle but, if you see minutely it is actually using stack.

- Anonymous August 31, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

void PrintQ( MyStack<int> S ){
			if(S.empty()){
				return;
			}
			int temp = S.pop();
			PrintQ(S);
			cout<<temp<<"\n";
		}

Do let me know if this is not the case.

Although, if you observe minutely then it uses two stacks

- coderishere August 31, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

// the idea is to add the push-ed item to the queue and then rotate the queue

package com.home.careercup;

import java.util.LinkedList;
import java.util.Queue;

public class StackWithQueue <T>{
    private Queue <T> q = new LinkedList<>();
    void push (T e) {
        q.offer(e);
        if (q.size() > 1){
            for (int i = 0; i <q.size() -1 ; i++) {
                q.offer(q.remove());
            }
        }

    }
    T pop (){
        return q.isEmpty() ? null : q.remove();
    }

    public static void main(String[] args) {
        StackWithQueue <String> stack  = new StackWithQueue<>();
        for (String s: new String [] {"a", "b", "c", "d"}){
            stack.push(s);
        }
        String pop =null;
        while ( null !=(pop = stack.pop())){
            System.out.println(pop);
        }
    }
}

- Makarand September 01, 2017 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More