Bloomberg LP Interview Question for Software Engineer / Developers






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

1. Get length of the loop first :- take two pointers from head, one increment with one second with two, at some point these two pointer will meet(means, that two pointers are in the loop). From that point increment one pointer only and count the size of loop.
2.find the loop point(merging point) : - we know size of loop.Take two pointers at head increment one pointer until the difference between two pointers is equal to size of the loop.then after increment both pointers until they meet(they will meet at merging point).
3.find the kth node from the end of loop:- Two pointers after step-2 will be at merge point. increment one pointer until it goes n, (where n = (size of loop) - k ). that will give kth node form the end of loop

- Balaji Yalamarthi December 28, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I have no idea why you would want to go to so much trouble!
1) Have two pointers pointing to the head of the loop or the starting point of the loop.
2) Recursively call the function with the head pointer and each time ptr->next. Do this until ptr->next == head.
3) Now you have reached the last node in the loop.
4) Now start unwinding the recursion and keep a counter as to how many recursive calls have been undone.
5) When this count == k (k th elt from last), you have your node.

- nebu January 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good algorithm but how do you find the starting point of the loop ?

- abhimanipal February 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@balaji.....neat solution...

- Neerav March 13, 2010 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

@nebu..

How can you compare with the head when head is not the part of the loop! The question is corrected that only 3,4,5 are in a loop

- Anonymous December 14, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@balaji's Soln:
How do we know the size of the loop?

- Anonymous December 14, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@balaji: ignore above comment...neat soln!

- Anonymous December 14, 2010 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

The formatting in the original question got messed up when I posted.

Node 5 should be pointing to Node 3.

- kcoder December 19, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This does not exist in case of single link list. suppose 3 is pointing to head and making loop . i.e either 3 points to head or 4 not both, 3 can not point two node at a time.

- nks December 18, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

pretty good!!

- Anonymous December 28, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Awesome algo Balaji...

- RajiniHassan January 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Get the size of the list, say n.
2. You have a constant value from end, say k.

Start iterating over the list from 0 to n-k+1 th position to get your element.

e.g. We have a list "21 32 43 24 15 12"
1. The size of array would be 5. i.e. n = 6.
2. We want 3rd element from end. i.e. k = 3.
So the desired position is n-k+1 i.e. 6-3+1 = 4th element i.e. 24.

- akashcpatel January 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The 'loopNode' will give the nth node.

}}}
public static List loopNode(List head, int n) {
List mergeNode = findMerge(head);
int loopSize = countLoop(mergeNode);

List forwardNode = head;
List backNode;

for (int i = 0; i < loopSize; i++) {
forwardNode = forwardNode.next;
}
for (backNode = head; forwardNode != backNode; forwardNode = forwardNode.next, backNode = backNode.next) {
}
for (int i = 0; i < n; i++) {
backNode = backNode.next;
}
System.out.println(backNode.value);
return backNode;
}
}}}

This function counts the size of the loop.
}}}
public static int countLoop(List node) {
int count = 0;
if(node==null){return 0;};
List start = node.next;
while (start != node) {
count++;
start = start.next;
}
return ++count;
}
}}}

This function is find some node lying inside the loop.

public static List findMerge(List head) {
		List fastNode;
		List slowNode = null;

		if (head == null || head.next == null) {
			return head;
		}
		slowNode = head;
		fastNode = slowNode;

		for (; fastNode != null; fastNode = fastNode.next, slowNode = slowNode.next) {
			fastNode = fastNode.next;
			if(fastNode == null){
				return null;
			}
			if (slowNode == fastNode) {
				return slowNode;
			}
		}
		return null;
	}

- Abdul Malik Mansoor February 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What is "end of loop"? How can a loop have an end? Its like a balloon on a thread.

- Mahesh February 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

awsome algo

- Anonymous February 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If there is a loop existing, there will be no node after the loop.

- Xiao February 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Xiao: Exactly. So in a singly linked list, if there is a loop, the last node has to be the end of the loop. So doesn't this question actually reduce to finding the n-th node from the end of the loop ?

- Bandicoot March 28, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

first find the beginning of loop: use two pointers, one moves one node each time, another one moves two nodes each time. after they meet, place one of them at head, move them with speed of one together. this time when they meet, you find the beginning of loop.

then use two pointers, one moves 5 steps ahead, they all begin from the beginning of loop. the slower pointer points to the place when the faster one reaches the beginning again.

- Yimin September 10, 2010 | 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