Microsoft Interview Question for Software Engineer / Developers


Team: Global Foundation Services
Country: United States
Interview Type: In-Person




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

This can be done recursively for any m x n matrix. It does not matter if m and n are same or not.

I am giving the code for a singly linked list, but this can easily be extended to doubly linked list:

C#:

public class Node
{
	public int data;
	public Node right;
	public Node down;
}
private Node head;


// Wrapper method that calls the recursive method
// to create linked list out of matrix
public void ProcessMatrix(int[][] matrix)
{
	head=ConvertToLL(matrix,0,0);
}

// i=row, j=col
public Node ConvertToLL(int[][] matrix, int i, int j)
{
	if(i>matrix.Length) return null;	// i > available rows
	if(j>matrix[i].Length) return null;	// j > available cols
	
	Node node=new Node() { data=matrix[i][j] };  // using automatic initializers
	node.right=CovertToLL(matrix, i, j+1);
	node.down=ConvertToLL(matrix, i+1, j);

	return node;
}

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

This can be done recursively for any m x n matrix. It does not matter if m and n are same or not.

I am giving the code for a singly linked list, but this can easily be extended to doubly linked list:

C#:

public class Node
{
	public int data;
	public Node right;
	public Node down;
}
private Node head;

public Dictionary<string,Node> allNodes;

// Wrapper method that calls the recursive method
// to create linked list out of matrix
public void ProcessMatrix(int[][] matrix)
{
	allNodes=new Dictionary<string, Node>();
	head=ConvertToLL(matrix,0,0);
}

// i=row, j=col
public Node ConvertToLL(int[][] matrix, int i, int j)
{
	if(i>matrix.Length) return null;	// i > available rows
	if(j>matrix[i].Length) return null;	// j > available cols
	
	int key = i.ToString() + "," + j.ToString();
	if(allNodes.ContainsKey(key)) return allNodes[key];

	Node node=new Node() { data=matrix[i][j] };  // using automatic initializers
	allNodes[key]=node;
	node.right=CovertToLL(matrix, i, j+1);
	node.down=ConvertToLL(matrix, i+1, j);

	return node;
}

- Anonymous January 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Small problem in above code. The recursive call will create new nodes rather than reusing existing nodes created by other recursive calls.

Below code, fixes that by using hashtables to cache nodes

C#:

public class Node
{
	public int data;
	public Node right;
	public Node down;
}
private Node head;

public Dictionary<string,Node> allNodes;

// Wrapper method that calls the recursive method
// to create linked list out of matrix
public void ProcessMatrix(int[][] matrix)
{
	allNodes=new Dictionary<string, Node>();
	head=ConvertToLL(matrix,0,0);
}

// i=row, j=col
public Node ConvertToLL(int[][] matrix, int i, int j)
{
	if(i>matrix.Length) return null;	// i > available rows
	if(j>matrix[i].Length) return null;	// j > available cols
	
	int key = i.ToString() + "," + j.ToString();
	if(allNodes.ContainsKey(key)) return allNodes[key];

	Node node=new Node() { data=matrix[i][j] };  // using automatic initializers
	allNodes[key]=node;
	node.right=CovertToLL(matrix, i, j+1);
	node.down=ConvertToLL(matrix, i+1, j);

	return node;
}

- Anonymous January 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Clean and simple. Also as a side effect of using Hashtable, your running time improves, since it also acts as a memoization store.

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

wtf.just a copy paste of the above code.

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

int key = i.ToString() + "," + j.ToString();

is this even possible..?? Converting to string and assigning to int..?
Correct me if I am wrong.

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

public static Node Convert(int[,] matrix, int m, int n)
    {
        var nodes = new Node[m, n];
        
        for (var i = 0; i < m; i++)
        {
            for (var j = 0; j < n; j++)
            {
                nodes[i, j] = new Node(matrix[i, j]);
            }
        }
    
        for (var i = 0; i < m - 1; i++)
        {
            for (var j = 0; j < n - 1; j++)
            {
                nodes[i, j].Right = nodes[i, j + 1];
                nodes[i, j].Down= nodes[i + 1, j];
            }
        }
    
        return nodes[0, 0];
    }

I don't see a reason for using recursion

- Rannn August 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

My code :

//
//  main.cpp
//  ListListFromMatrix
//
//  Created by Srikant Aggarwal on 10/01/12.
//  Copyright 2012 NSIT. All rights reserved.
//

#include <iostream>

using namespace std;

typedef struct node
{
    int data;
    struct node *right;
    struct node *down;
}node;

int main (int argc, const char * argv[])
{
    int n, m;
    int i = 0;
    
    cin >> n >> m;
    
    int **arr = new int *[n];
    while(i < n)
    {
        arr[i] = new int[m];
        i++;
    }
    
    int j = 0;
    i = 0;
    
    while(i < n)
    {
        while(j < m)
        {
            cin >> arr[i][j];
            j++;
        }
        j = 0;
        i++;
    }
    
    node *parent = NULL, *child = NULL;
    node *start = NULL;
    
    i = 0;
    while(i < n)
    {
        j = 0;
        node *head = NULL;
        while(j < m)
        {
            node *temp = new node;
            temp->data = arr[i][j];
            temp->right = temp->down = NULL;
            
            if(head == NULL)
            {
                head = temp;
                if(start == NULL)
                    start = head;
            }
            else
                child->right = temp;
                
            if(parent != NULL)
            {
                parent->down = temp;
                parent = parent->right;
            }
            
            child = temp;
            j++;
        }
        
        parent = head;
        j = 0;
        i++;
    }
    
    node *curr_ver_node = start;
    while(curr_ver_node != NULL)
    {
        node *curr_hor_node = curr_ver_node;
        while(curr_hor_node != NULL)
        {
            cout << curr_hor_node->data << " ";
            curr_hor_node = curr_hor_node->right;
        }
        cout << endl;
        curr_ver_node = curr_ver_node->down;
    }
    
    return 0;
}

- Srikant Aggarwal January 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include "stdafx.h"
#include <cstring>
#include <cstdlib>
#include <cstdio>

struct Node
{
double data;
Node *right;
Node *down;
Node()
{
right =NULL;
down =NULL;
}
};
int m, n;
Node* convertMaxtrixToList(double matrix[4][4])
{
Node *root;
Node *last_top_left_node =NULL;
for (int i=0;i<m;i++)
{
Node *top_left_node =new Node();
top_left_node->data =matrix[i][i];
if (i==0)
root =top_left_node;
Node *pre_node =top_left_node;
for (int j=i+1;j<n;j++)
{
Node *node =new Node();
node->data = matrix[i][j];
pre_node->right =node;
pre_node =node;

}
pre_node =top_left_node;
for (int j =i+1;j<n;j++)
{
Node *node =new Node();
node->data = matrix[j][i];
pre_node->down =node;
pre_node =node;
}
if (last_top_left_node)
{
Node *tmp =last_top_left_node ->right;
for (Node *node =top_left_node;node;node=node->right, tmp =tmp->right)
{
tmp->down =node;
}

tmp=last_top_left_node ->down;
for (Node *node =top_left_node;node;node=node->down, tmp =tmp->down)
{
tmp->right =node;
}
}
last_top_left_node =top_left_node;
}
return root;
}

void print(Node *root)
{
for (Node *i =root;i;i=i->down)
{
for (Node *j=i;j;j=j->right)
{
printf("%lf ", j->data);
}
printf("\n");
}
}

int main ()
{
double matrix[4][4];
memset(matrix, 0, sizeof matrix);
m =4;
n =4;
Node *root = convertMaxtrixToList(matrix);
print(root);
return 0;
}

- bo hou January 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

this boils down to serializing a linked list. the code and explanation is there in 'programming interviews exposed'

- smffap January 14, 2012 | 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