Bloomberg LP Interview Question for Financial Software Developers






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

using two pointers

p1=head;
p2=head->next;
while(p2!=null) 
{   
    p1=p1->next;
    p2=p2->next
    if(p2!=null) p2=p2->next;
}
return p1;

- xiaogold March 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

excellent soln.. thanks :)

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

nice

- Anonymous June 04, 2010 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

Logic is - increment first pointer, and increment second pointer twice as fast. When second pointer reaches the end, the first one will point to the middle ( because it was moving 2 times slower).

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

typedef struct Node {
int data;
Node* next;
};

Node* getMiddle(Node* head) {
int count = 0;
Node* middle = head;
Node* curr = head;
while (curr != NULL) {
if (count == 2) {
middle = middle->next;
count = 0;
}
curr = curr->next;
count++;
}

return middle;
}

- mohit March 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

middle=head;
last=head;
int count = 0;
while(last)
{
last = last->next;
if(count % 2)
{
middle = middle->next
}
++count;
}
return middle;

- stepany March 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What kind of Singly linked list are you referring to? Please clarify ...

1. Basic singly linkedlist
2. Double "ended" sinlgly linked list (pointer to the head and the tail elements of the list
3. Circular singly linkedlist
4. Singly linked list w/ a sentinel

- Brian - March 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Is it not obvious that if none of them is mentioned, it is a basic singly linked list ?

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

Can someone explain the logic? or preferable approach in Java

- Anonymous April 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

static void findMiddleElement(LinkedListNode head) {
// LinkedListNode head = new LinkedListNode(1);
// head.appendToTail(2);
// head.appendToTail(3);
// head.appendToTail(4);
// head.appendToTail(5);
// head.appendToTail(6);
// head.appendToTail(7);
// head.appendToTail(8);
// head.appendToTail(9);
// head.appendToTail(10);
// head.appendToTail(11);

LinkedListNode current = head;
LinkedListNode middle = head;

while (current.next != null) {
current = current.next;
if (current.next != null) {
current = current.next;
middle = middle.next;
}
}
System.out.println("Middle Element :" + middle.value);
}


public class LinkedListNode {
public LinkedListNode next;
public LinkedListNode previous;
public int value;

public LinkedListNode(int value) {
this.value = value;
}

public void appendToTail(int value) {
LinkedListNode end = new LinkedListNode(value);
LinkedListNode temp = this;
while (temp.next != null) {
temp = temp.next;
}
temp.next = end;
}}

- static void findMiddleElement(LinkedListNode head) { // LinkedListNode head = new LinkedListNode(1); // head.appendToTail(2); // head.appendToTail(3); // head.appendToTail(4); // head.appendToTail(5); // head.appendToTail(6); // head.appendToTail(7); // head.appendToTail(8); // head.appendToTail(9); // head.appendToTail(10); // head.appendToTail(11); LinkedListNode current = head; LinkedListNode middle = head; while (current.next != null) { current = current.next; if (current.next != null) { current = current.next; middle = middle.next; } } System.out.println("Middle Element :" + middle.value); } public class LinkedListNode { public LinkedListNode next; public LinkedListNode previous; public int value; public LinkedListNode(int value) { this.value = value; } public void appendToTail(int value) { LinkedListNode end = new LinkedListNode(value); LinkedListNode temp = this; while (temp.next != null) { temp = temp.next; } temp.next = end; }} January 10, 2020 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Wow...! That's the best solution.

- Karthik December 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is the code for java programmers

static void findMiddleElement(LinkedListNode head) {
		// LinkedListNode head = new LinkedListNode(1);
		// head.appendToTail(2);
		// head.appendToTail(3);
		// head.appendToTail(4);
		// head.appendToTail(5);
		// head.appendToTail(6);
		// head.appendToTail(7);
		// head.appendToTail(8);
		// head.appendToTail(9);
		// head.appendToTail(10);
		// head.appendToTail(11);
		
		LinkedListNode current = head;
		LinkedListNode middle = head;

		while (current.next != null) {
			current = current.next;
			if (current.next != null) {
				current = current.next;
				middle = middle.next;
			}
		}
		System.out.println("Middle Element :" + middle.value);
	}


public class LinkedListNode {
	public LinkedListNode next;
	public LinkedListNode previous;
	public int value;

	public LinkedListNode(int value) {
		this.value = value;
	}

	public void appendToTail(int value) {
		LinkedListNode end = new LinkedListNode(value);
		LinkedListNode temp = this;
		while (temp.next != null) {
			temp = temp.next;
		}
		temp.next = end;
	}}

- Fresh_man April 13, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def solution_9

- Anonymous April 14, 2019 | 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