Microsoft Interview Question
Software Engineer in TestsCountry: United States
Interview Type: In-Person
The Second Part can b solved by reversing the linklist(in one traversal) and then performing the addition operation .
Dont forget to multiply by 10, 100 etc for the corresponding tens , hundreds , etc place
You dont need the count also.
num say 2->4->6->8 + 5->6->7
so in one go calculate the first number:
2 +
2*10 + 4 = 24 +
24+10 +6 = 246 +
246*10 + 8 =2468
similary the second num as 567
add them by normal addition
Cheers !!
above scenario will work for small values,but if we have the large number being stored in a list then above scenario wont work out..
def convert_to_LL_util(num):
if num:
last_digit = num % 10
node = ListNode(last_digit)
head, tail = convert_to_LL_util(num / 10)
if not tail:
return node, node
tail.next = node
return head, node
else:
return None, None
def convert_to_LL(num):
sign = 1
if num < 0:
sign = -1
h, _ = convert_to_LL_util(num * sign)
h.val *= sign
return h
public class Integer2LinkedList {
public static void main(String[] args)
{
int input = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the integer : ");
input = sc.nextInt();
convert2LL(input);
}
public static void convert2LL(int input)
{
LinkedList<Integer> list = new LinkedList<Integer>();
boolean neg = false;
int j = 0;
// int k = 0;
if(input < 0)
{
neg = true;
input = -(input);
}
int i = input;
while(i>0) {
j = i%10;
i = i / 10;
list.addFirst(j);
}
if(neg)
{
list.set(0, -(list.getFirst()));
}
for(int k = 0; k<list.size(); k++)
{
System.out.print(list.get(k));
}
}
}
public class Integer2LinkedList {
public static void main(String[] args)
{
int input = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the integer : ");
input = sc.nextInt();
convert2LL(input);
}
public static void convert2LL(int input)
{
LinkedList<Integer> list = new LinkedList<Integer>();
boolean neg = false;
int j = 0;
// int k = 0;
if(input < 0)
{
neg = true;
input = -(input);
}
int i = input;
while(i>0) {
j = i%10;
i = i / 10;
list.addFirst(j);
}
if(neg)
{
list.set(0, -(list.getFirst()));
}
for(int k = 0; k<list.size(); k++)
{
System.out.print(list.get(k));
}
}
}
working code for both part (a) and (b) is:
- Gupta July 05, 2012i think this is self understandable code. you just go through this . let me know if some one has problem with this code.
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
/* Link list node */
struct node
{
int data;
struct node* next;
};
/* Function to reverse the linked list */
static void reverse(struct node** head_ref)
{
struct node* prev = NULL;
struct node* current = *head_ref;
struct node* next;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head_ref = prev;
}
/* Function to push a node */
void push(struct node** head_ref, int new_data)
{
/* allocate node */
struct node* new_node =(struct node*) malloc(sizeof(struct node));
/* put in the data */
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
struct node* addition (struct node* temp1, struct node* temp2)
{
struct node* prev = NULL;
int carry = 0,a,b,result;
while (temp1 || temp2) //while both lists exist
{
result = carry;
if(temp1)
result+=temp1->data;
if(temp2)
result+=temp2->data;
carry=result/10;
struct node * newnode = (struct node*) malloc (sizeof(struct node));
newnode->data=(result)%10;
newnode->next = prev;
prev=newnode;
if(temp1)
temp1=temp1->next;
if(temp2)
temp2=temp2->next;
}
return prev;
}
void printList(struct node *node)
{
while(node != NULL)
{
printf("%d ", node->data);
node = node->next;
}
}
/* Drier program to test above function*/
int main(void)
{
/* Start with the empty list */
struct node* head = NULL;
struct node* head1 = NULL;
struct node* head2 = NULL;
int num1;
int num2;
printf("Enter number 1 ::");
scanf("%d",&num1);
printf("Enter number 2::");
scanf("%d",&num2);
int rem = 0;
if(num1 > 0 && num2 > 0)
{
while(num1 > 0)
{
rem = num1%10;
push(&head1,rem);
num1 = num1/10;
}
while(num2 >0)
{
rem = num2%10;
push(&head2,rem);
num2 = num2/10;
}
printf("list 1 is \t");
printList(head1);
printf("\n");
reverse(&head1);
printf("list 1 reverse is \t");
printList(head1);
printf("\n");
printf("list 2 is \t");
printList(head2);
printf("\n");
reverse(&head2);
head=addition(head1,head2);
printf("resultant list is \t");
printList(head);
printf("\n");
}
else
printf("you have entered wrong number , please enter again");
getch();
}