## Ebay Interview Question for Software Developers

• 0

Team: OpenStack
Country: United States
Interview Type: In-Person

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

``````//Lets have 0th elements of lists containing lower digits.
public static Short BASE = 10000

public List<Short> addTwoHugeNumbers(List<Short> a, List<Short> b) {
Iterator<Short> iterA = a.iterator();
Iterator<Short> iterB = b.iterator();

Short overflow = 0;

while(true) {
Short nextA = null;
Short nextB = null;
if (iterA.hasNext()) {
nextA = iterA.next();
}
if (iterB.hasNext()) {
nextB = iterB.next();
}
if (nextA == null && nextB == null) {
//no more digits to add, worl done
break;
}
if (nextA == null) {
nextA = 0;
}
if (nextB == null) {
nextB = 0;
}
Short nextSum = nextA + nextB + overflow;
Short nextDigit = nextSum % BASE;
overflow = nextSum / BASE;
}
if (overflow != 0) {
}
return result;
}``````

}

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

Add the node values and send the carry over to the next node.
Reverse the result list

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

``````ListNode sumList(ListNode l1, ListNode l2) {
int BASE = 10000;
if(l1 == null) return l2;
if(l2 == null) return l1;
ListNode l1Rev = reverse(l1);
ListNode l2Rev = reverse(l2);
ListNode dummyNode = new ListNode(0);
ListNode prev = dummyNode;
int carry = 0;
while(l1Rev != null && l2Rev != null) {
int l1Val = (l1Rev != null) ? l1Rev.val : 0;
int l2Val = (l2Rev != null) ? l2Rev.val : 0;
int sum = l1Val + l2Val + carry;
if(sum >= BASE)
carry = sum / BASE;
prev.next = new ListNode(sum % BASE)
l1Rev = (l1Rev.next == null) ? l1Rev : l1Rev.next;
l2Rev = (l2Rev.next == null) ? l2Rev : l2Rev.next;
}
if(carry >0)
prev.next = new ListNode(1);
return reverse(dummyNode.next);
}

}
}``````

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

``````//Language: Scala

val NoCarry = 0
val FixedLength = 4

def fourDigitPerTerm(ls: List[Int]): Boolean = ls match {
case Nil => true
case h::t => if (h.toString.length == FixedLength) fourDigitPerTerm(t) else false
}

def addNumbers(ls1: List[Int], ls2: List[Int]) = {
require(fourDigitPerTerm(ls1++ls2))
val ls1Reversed = ls1.reverse
val ls2Reversed = ls2.reverse

ls2: List[Int],
carry: Int = NoCarry,
acc: List[String] = Nil): List[String] = (ls1, ls2) match {

case (Nil, Nil) if carry == NoCarry => acc
case (Nil, Nil) => s"000\$carry"::acc
case (_, _) => val elem = carry +
val elemLength = elem.toString.length
if (elemLength > FixedLength) (1, elem.toString.tail)
else (NoCarry, elem.toString)
if(ls1.isEmpty) Nil else ls1.tail,
if(ls2.isEmpty) Nil else ls2.tail, c,
}
}

}``````

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

Stores values in separate stacks and then keep popping elements. We should use stacks in order to keep original LinkedLists intact. (if that's the business rule!)

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.