National Instruments Interview Question for Software Engineer in Tests






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

hey, we can form complete decimal number by traversing from head to last node and then taking difference between the two and again breaking them in single digits & storing them in third linked list.....

- vick August 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use stack

- DashDash July 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

use recursion.

pad zeroes to either of the linked list so tat they are equal. here is the algo after that.


sum(n1, n2, &10, &false);

Sum(n1, n2, &temp, &carry)
{
if(!n1 || !n2)
return;

if(carry)
{
if(n1 == 0)
cout<< (n1->data + (--temp)) - n2->data;
else
{
n1->data --;
carry = false;
temp = 10;
}
}

if(n1->data < n1->data)
{
cout<< (n1->data + temp) - n2->data;
carry = true;
temp--;
}
else
cout<< (n1->data) - (n2->data);

sum(n1->next, n2->next, temp, carry);

}

- Nit June 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

doesn't stack means we are reversing?

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

I also thought of using stack. But since the question says - "very large" linked list, I don't think using stack will impress the interviewer. We will be duplicating the memory requirement if we use the stack. I hope there is a better alternative to that approach.

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

ideone[dot]com/Nq3hI

O(n) time and space. I did not choose to use recursion because the question says "very long lists" - which would mean too much of space and func call overhead.

- TheRandomGuy July 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

can u please explain the algorithm u implemented ?

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

fixed an edge case - ideone[dot]com/MqMZS

- TheRandomGuy July 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think Your code doesn't handle cases like 100000 -1

- Bharath Venk July 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

ideone[dot]com/btUC3

- C++ beginner July 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

First of all you are storing the numbers in arrays.
For large numbers, this is definitely not allowed.

Further, every one knows subtraction.
The following piece of code :

c[i]=(a[i]+10)-b[i];
a[i-1]=a[i-1]-1;

is the challenging part since in linked list it is difficult to achieve.

So I think this solution is unacceptable.

- ABC July 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

i am new to this forum so please give feedback on the above code

- c++ beginner July 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

check this out. I used doubly linked list to collect the result. ideone[dot]com/aYEzA

- freelancer July 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Using Doubly Linked list is obviously NOT allowed.

- ABC July 06, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

if allowed to change link list B then

add zeroes at the start of B till both link list length is equal
then start subtracting from head till end.

at any point if A.data>=B.data then the resultant link list will have a node of value
A.data-Ba.data
else if B.data is greater, resultant will have a node of value 10+A.data-B.data. and the value of previous node in the resultant link list will be decremented by 1. ( will need to maintain a previous pointer)

after traversing the whole link list, resultant link list will have the answer.

restore link list B

since we are subtracting only 1 number from A , the borrow required will not propoagate more than 1 digit to right. hence we can subtract from head as well.

eg
A 5 4 2 0 0 9 0 0
B 1 9 9

pad zeroes to B

A 5 4 2 0 0 9 0 0
B 0 0 0 0 0 1 9 9
C is resultant link list

start from head
5>0 C 5
4>0 C 5 4
2>0 C 5 4 2
0>=0 C 5 4 2 0
0>=0 C 5 4 2 0 0
9>1 C 5 4 2 0 0 8
0<9 hence v=next value will be 10 + 0 -9 =1 and prev node is decremented. hence 8 becomes 7 C is 5 4 2 0 0 7 1
0<9 similar to above value is 10 +0 -9 =1 and previous 1 becomes 0

C is 5 4 2 0 0 7 0 1

this is assuming A>B if not which we can come to know while examining the head values
call same function with the lists swapped and will need to append - sign at head.

- nik July 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

edit

"since we are subtracting only 1 number from A , the borrow required will not propoagate more than 1 digit to right."

typo. i meant left and not right.

- nik July 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What about 10000 - 1 ?
It propagate all the way to the left.

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

Why cannt we convert it in to decimal subtract it and then convert back to linked list

converting will take only o(n) and the subtracting o(1) and again converting o(n).

- Sandy August 18, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

that won't work for a very long linked list, e.g. a long type variable cannot hold 9999999999999999999999999999

- giftzyx February 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

find the length of another LL (to be Subtracted)say 3 in this case
take a ponter p1 pointing to 1st element of list A and another pointer p2 pointing to 3rd element of list A.when p2 will reach end p1 will at third last number and now we can easily subtract list B from it...

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

does dat work with 10000000000000-1 ??

- rithwik March 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Since, the linked lists can be very large, we obviously cannot convert them into a number and subtract using a unary minus. So the trick to solve this question is to traverse the list and subtract the value of list1 with the value of list2 and also subtract the borrow. If the result is negative, add 10 to the final result.

- mitshubh October 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

;lk;lk

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

let we are subtracting (A - B) List. So We can create an array having entries of index where numbers in List A is greater then List B. And we can subtract numbers acording to that array entries. This will take total of O(n)Time and O(n) Space.

- Ankur Singh November 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

let we are subtracting (A - B) List. So We can create an array having entries of index where numbers in List A is greater then List B. And we can subtract numbers acording to that array entries. This will take total of O(n)Time and O(n) Space.

- Ankur Singh November 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I was thinking something along the lines of.

Start identifying the both ll's length at the same time.
If you see one of them ending
stop
////now you know the length of the smaller one. Let = X
Now identify the X digits from the end for the bigger ll. (Using one pointer and another pointer legging behind by X traversing once. It's O(n))
Do math.

From what I see here complexity is minimized, actually its O(n) and no extra space.

- Ash February 12, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I was thinking something along the lines of.

Start identifying the both ll's length at the same time.
If you see one of them ending
stop
////now you know the length of the smaller one. Let = X
Now identify the X digits from the end for the bigger ll. (Using one pointer and another pointer legging behind by X traversing once. It's O(n))
Do math.

From what I see here complexity is minimized, actually its O(n) and no extra space.

- Ash February 12, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//find the length of both list l1, l2 
//append l1-l2 nodes in the second list  in front
fun(list a, list b){
	if(a==null)
		return 0;
	int borrow=fun(a.next,b.next,c)
	Node newNode=new Node();
	if(a.data>b.data){
		newNode.data=a.data-b.data-borrow;
		newNode.next=c;//front insertion
		c=newNode;
		return 0;
	}
	else if(a.data==b.data){
		if(borrow==0){
			newNode.data=0;
			newNode.next=c;
		    c=newNode;
			return 0;
		}
		else{
			newNode.data=9;
			newNode.next=c;
		    c=newNode;
			return 1;
		}
		else{
			newNode.data=a.data+10-b.data-borrow
			newNode.next=c;
		    c=newNode;
			return 1;
		}
	}

}

- samyak jain September 21, 2015 | 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