## Bloomberg LP Interview Question for Senior Software Development Engineers

• 0

Country: United States
Interview Type: Phone Interview

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

Maintain a variable for totalSum and another variable for carry. Add the value based on the head of the linked lists. Keep iterating while there is another node in the linked list or there is a carry. TC:- O(n+m) where n and m are the size of the linked lists. SC:- O(n+m) since we are constructing a new linked list and returning it. My solution in Python:

``````def addTwoNums(head1, head2):
carry = 0
curr = dummy = Node(None)

totalSum = 0

totalSum += carry
carry = totalSum // 10
curr.next = Node(totalSum % 10)
curr = curr.next

return dummy.next``````

Test code:

``````# Test code

class Node:
def __init__(self, value):
self.value = value
self.next = None

num1 = Node(4)
num1.next = Node(5)
num1.next.next = Node(6)

num2 = Node(7)
num2.next = Node(8)
num2.next.next = Node(9)

printList(num1)
print('\n+')
printList(num2)
print('\n' + ('-'*10))
printList(result)

num1 = Node(4)
num1.next = Node(5)

num2 = Node(1)
num2.next = Node(2)
num2.next.next = Node(3)

print('\n\n')
printList(num1)
print('\n+')
printList(num2)
print('\n' + ('-'*10))
printList(result)``````

Output:

``````4->5->6->
+
7->8->9->
----------
1->4->6->1->

4->5->
+
1->2->3->
----------
5->7->3->``````

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

Solution in java:

``````import java.util.Iterator;

/**
* Solution:
* Step 1: Sort LinkedList in descending order
* Step 2: Check x[i] + y[i] + carryover > 10 ? assign carryForward = 1, substract 10 from result
* Step 3: insert result in resulting LinkedList
* Step 4: insert final carry over
* @author sk14882
*
*/

protected LinkedList<Integer> sum(int x[], int[] y) {

for (Integer i: x) {
}
System.out.println("Input l1[] -> " +l1);

for (Integer i: y) {
}
System.out.println("Input l2[] -> " + l2);

Iterator<Integer> i1r = l1.descendingIterator();
Iterator<Integer> i2r = l2.descendingIterator();

int carryForward=0;
while(i1r.hasNext() || i2r.hasNext()) {
Integer i = (i1r.hasNext() ? i1r.next() : 0);
Integer j = (i2r.hasNext() ? i2r.next() : 0);
int result = i+j+carryForward;
if(result >= 10) {
carryForward = 1;
result = result - 10;
} else {
carryForward = 0; //reset
}
}

if(carryForward > 0) {
}

System.out.println("Result r[] -> " + lr);

return lr;
}
}``````

Test Code:

``````package com.hacker.rank.challenges;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

import org.junit.Test;

private static int[] x1 = new int[] {1,2,3};
private static int[] y1 = new int [] {4,5,6};

private static int[] x2 = new int[] {1,2,3};
private static int[] y2 = new int[] {4,5};

private static int[] x3 = new int[] {4,5,6};
private static int[] y3 = new int[] {7,8,9};

@Test
public void testSum() throws Exception {

assertThat(sample.sum(x1, y1), equalTo(lr1));

assertThat(sample.sum(x2, y2), equalTo(lr2));

assertThat(sample.sum(x3, y3), equalTo(lr3));
}

}``````

Output:

``````Input l1[] -> [1, 2, 3]
Input l2[] -> [4, 5, 6]
Result r[] -> [5, 7, 9]
Input l1[] -> [1, 2, 3]
Input l2[] -> [4, 5]
Result r[] -> [1, 6, 8]
Input l1[] -> [4, 5, 6]
Input l2[] -> [7, 8, 9]
Result r[] -> [1, 2, 4, 5]``````

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

The Java solution above seems like a lot of code ... Here's a simpler O(n) version:

String sum = (parseValue(a) + parseValue(b)) + "";

for(Character digit : sum.toCharArray())

return result;
}

public static int parseValue(LinkedList<Integer> list) {

if(list == null || list.isEmpty()) return 0;

StringBuilder sb = new StringBuilder();
for(int digit : list)
sb.append(digit);

return Integer.parseInt(sb.toString());
}

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

``````struct Node* reverse(struct Node* head) {
struct Node* prev = 0;
while(p) {
p = p->next;
}
}

struct Node* suml(struct Node* f, struct Node* s) {
f = reverse(f);
s = reverse(s);
int carry = 0;
while(f || s || carry > 0) {
if(f) {
carry += f->data;
f = f->next;
}
if(s) {
carry += s->data;
s = s->next;
}
struct Node* tmp = malloc(sizeof(struct Node));
tmp->data = carry % 10;
carry /= 10;
}
f = reverse(f);
s = reverse(s);
}``````

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

``````struct Node* reverse(struct Node* head) {
struct Node* prev = 0;
while(p) {
p = p->next;
}
}

struct Node* suml(struct Node* f, struct Node* s) {
f = reverse(f);
s = reverse(s);
int carry = 0;
while(f || s || carry > 0) {
if(f) {
carry += f->data;
f = f->next;
}
if(s) {
carry += s->data;
s = s->next;
}
struct Node* tmp = malloc(sizeof(struct Node));
tmp->data = carry % 10;
carry /= 10;
}
f = reverse(f);
s = reverse(s);
}``````

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

``````Node *Add(Node *h1, Node *h2)
{
int sum = 0;

while ( h1 != NULL || h2 != NULL)
{
Node* sumNode = new Node;

sum = 0;
if( h1 != NULL)
{
sum += h1->value;
h1 = h1->next;
}

if( h2 != NULL)
{
sum += h2->value;
h2 = h2->next;
}

sumNode->value = sum;
sumNode->next = NULL;
curr->next = sumNode;
curr = sumNode;
}

}``````

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

C++ solution

``````#include <list>
using namespace std;

typedef list<int> IntList;

IntList::value_type getListValue(const IntList &intlist)
{
IntList::value_type value = 0;
for (IntList::const_iterator i = intlist.cbegin(), iEnd = intlist.cend(); i != iEnd; ++i) {
value *= 10;
value += *i;
}
return value;
}

IntList sumIntList(const IntList &i1, const IntList &i2)
{
IntList sumList;
for (int sum = getListValue(i1) + getListValue(i2); sum; sum /= 10) {
sumList.push_front(sum % 10);
}
return sumList;
}``````

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.

### 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.