Interview Question for Java Developers


Country: United States
Interview Type: Written Test




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

Just a Linked list should be fine to handle all these cases. Also, have a variable size to execute the include x y test case. pop() and push in O(1). and include operation in O(n) time.

- Anonymous June 14, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class SuperStack{
public:
	stack<int> st;
	unordered_map<int,int> inc_hm;
	int cur_inc;

	void push(int v){
		st.push(v);
		cur_inc = 0;
		print_top();
	}

	void pop(){
		if(inc_hm.find(st.size())!=inc_hm.end()) {
			cur_inc = inc_hm[st.size()];
			if(st.size()-1) inc_hm[st.size()-1] += inc_hm[st.size()];
			inc_hm.erase(st.size());
		}
		st.pop();
		print_top();
	}

	int inc(int h,int v){
		if(h > st.size()) h = st.size();
		inc_hm[h] += v;
		print_top();
	}

	void print_top(){
		if(!st.empty()) cout<<st.top() + inc_hm[st.size()]<<endl;
		else cout<<"EMPTY"<<endl;
	}
};

- petersil June 14, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class SuperStack{
public:
	stack<int> st;
	unordered_map<int,int> inc_hm;
	int cur_inc;

	void push(int v){
		st.push(v);
		cur_inc = 0;
		print_top();
	}

	void pop(){
		if(inc_hm.find(st.size())!=inc_hm.end()) {
			cur_inc = inc_hm[st.size()];
			if(st.size()-1) inc_hm[st.size()-1] += inc_hm[st.size()];
			inc_hm.erase(st.size());
		}
		st.pop();
		print_top();
	}

	int inc(int h,int v){
		if(h > st.size()) h = st.size();
		inc_hm[h] += v;
		print_top();
	}

	void print_top(){
		if(!st.empty()) cout<<st.top() + inc_hm[st.size()]<<endl;
		else cout<<"EMPTY"<<endl;
	}
};

- petersil June 14, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A vector (in C++) or an ArrayList (in Java) can be used to implement such a Stack, where push operations append to the end of the vector (e.g. with push_back), pop operations remove from the end (e.g. with pop_back) and inc operations perform updates starting from the 0 index (start of the vector)

- Stelios Sfakianakis September 13, 2018 | 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