Microsoft Interview Question for Software Developers

Country: India
Interview Type: Written Test

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

``````#include <iostream>

using namespace std;

class Node {
public:
Node(int val)
{
val_ = val;
next_ = NULL;
}
int val_;
Node *next_;
};

{
while (n) {
Node *next_n = NULL;
if (n->next_) {
next_n = n->next_->next_;
n->next_ = next_n;
}
if (next_n == NULL) {
}
n = next_n;
}
}

{
for (Node *n = head; n != NULL; n = n->next_) {
cout << n->val_ << "->";
}
cout << "\n";
}

int main()
{
Node n1(1), n2(2), n3(3), n4(4), n5(5), n6(6), n7(7), n8(8), n9(9);
n1.next_ = &n2;
n2.next_ = &n3;
n3.next_ = &n4;
n4.next_ = &n5;
n5.next_ = &n6;
n6.next_ = &n7;
n7.next_ = &n8;
n8.next_ = &n9;

Print(&n1);
AlternateReverse(&n1);
Print(&n1);
}``````

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

{
{
{
n.next = null;
if (temp != null){
temp.next = n;
return n;
}
}
else
}
else
return null;
}

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

Iterate through all list nodes and put the alternate nodes on to stack. Time complexity of O(N) and space complexity of S(N/2).

``````public Node ReverseAlternateNodes(Node head){

/*
Input: 1 -> 2 -> 3 -> 4 -> 5 -> 6
Output: 1 -> 3 - > 5 -> 6 -> 4 -> 2
*/

Stack<Node> stck = new Stack<Node>();

while (temp.Next != null){

var next = temp.Next.Next;
var reverseItem = temp.Next;

//Null out the reversedNode next item.
reverseItem.Next = null;
//Push the next node on to the stack;
stck.Push(reverseItem);

//Assign Next.Next to current next;
temp.Next = next;

if(temp.Next != null){
temp = temp.Next;
continue;
}
break;
}

while(stck.Count > 0){
temp.Next = stck.Pop();
//Console.WriteLine(temp.Next.Val);
temp = temp.Next;
}

}``````

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

``````public Node ReverseAlternateNodes(Node head){

/*
Input: 1 -> 2 -> 3 -> 4 -> 5 -> 6
Output: 1 -> 3 - > 5 -> 6 -> 4 -> 2
*/

Stack<Node> stck = new Stack<Node>();

while (temp.Next != null){

var next = temp.Next.Next;
var reverseItem = temp.Next;

//Null out the reversedNode next item.
reverseItem.Next = null;
//Push the next node on to the stack;
stck.Push(reverseItem);

//Assign Next.Next to current next;
temp.Next = next;

if(temp.Next != null){
temp = temp.Next;
continue;
}
break;
}

while(stck.Count > 0){
temp.Next = stck.Pop();
//Console.WriteLine(temp.Next.Val);
temp = temp.Next;
}

}``````

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.