Bloomberg LP Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

Stacks can be implemented using
1) vector
Complexity: pop is O(1). and for push, if resizing is needed O(N), otherwise, O(1). This approach uses not very memory efficient as vector occupy double the size of the number of elements.
2) Linked list:
Complexity is O(1) for push and pop. Memory efficient but the memory is not contiguous.
3) Deque (default implementation of the std library)
Complexity is O(1) for push and pop
Memory efficient (uses contiguous memory mostly, and may use dynamic memory if the size grows too large). Contiguous memory is not guaranteed.
4) Array (If the stack max size is known and fixed)
Complexity is O(1) for push and pop.

For unknown size stack, Deque is the most efficient approach regarding memory usage and complexity O(1) for insert and remove.

- Ali June 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Linked List.
O(1) push, pop.

- chriscow January 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Or vector...

- Anonymous January 30, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

or stack.

oh no. Now the linked list has a loop.

- Anonymous January 31, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
 * Homemade implementation
 */
public class Stack {

	class Node{
		String val;
		Node next;
	}

	private Node top;

	public void push(Node n) {
		n.next = top;
		top = n;
	}

	//Returns null for empty list
	public Node pop() {
		Node t = top;
		top = top.next;
		return t;
	}

	public void toString() {
		Node c = top;
		System.out.println("[");
		while(c != null) {
			System.out.println(c.val + ", ");
			c = c.next;
		}
		System.out.println("]");
	}

}

- tp February 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It is implemented using deque (double-ended queue) which is implemented using vector of vectors. Thus you get constant time push and pop O(1).
[If you use linked list how would you get O(1) for push and pop??]

- nharryp March 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Linkedlist is enough as push and pop happens at the same end (head), thus deque is superfluous.

- PeterT March 17, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes..my mistake. Linked List will be enough : insert at start(push) and delete from start (pop).

- nharryp November 04, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

we can use push front and pop front of a list STL container to achieve this

- jamie rahman May 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Constant Time O(1).

import java.util.*;
public class Stack {

	public static LinkedList<String> l = new LinkedList<String>();
	
	public static void push(String s, LinkedList<String> l){
		
		l.add(s);
	}
	
	public static String pop(LinkedList<String> l){
		
		String s = l.getLast();
		l.removeLast();
		return s;
		
	}
	
	public static void main(String[] args) {
		
		push("a",l);
		push("b",l);
		push("c",l);
		push("d",l);
		pop(l);
		
		System.out.println("Contents of the Stack " + l);

	}

}

- careeradmirer January 31, 2014 | 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