Microsoft Interview Question for Software Engineer in Tests


Country: United States
Interview Type: In-Person




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

working code for both part (a) and (b) is:
i 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();
}

- Gupta July 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

int initialnumber = 1234
int number = Abs(initialnumber)
While number > 10
BEGIN
mod = number % 10
Insert mod at head
number = number / 10
END

if initialnumber > 0
Insert number at head
else
Insert number * -1 at head

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

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

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

you really dont have to reverse the list, just getting the count of the list will do

- Raj_pandian July 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@ Raj_pandian: can u explain the use of count, coz the no. of traversals required in that case are more i guess.

- madhu August 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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 !!

- Anonymous July 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- PB July 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@PB: why it won't work for large numbers? Were you referring to overflow?

- needajob September 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This logic will fail even in case, where List is negative for ex: first list is -2468, you need to make your logic robust enough to handle this situation.

- Jayesh May 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- sumitgaur.iiita January 13, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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));
        }


    }


}

- Anonymous March 16, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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));
        }


    }

}

- KV March 16, 2018 | 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