Microsoft Interview Question for Software Engineer in Tests






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

use level-order traversal to write/read tree

- nim September 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

here is a code to serialize and de-serialize a binary search tree. The key idea here is to serialize the BST in pre-order.

#include "BinaryTree.h"
#include <fstream>
#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;

static void serializeImp(btree *p, ofstream& sFile, char *buf)
{
        if(p == NULL)
                return;

        sprintf(buf, "%d", p->data);
        for(int i = 0; buf[i] != '\0'; i++)
                sFile.put(buf[i]);

        sFile.put(' ');
        serializeImp(p->left, sFile, buf);
        serializeImp(p->right, sFile, buf);
}

void serializeBST(btree *p, char *filename)
{
        char buffer[34];
        ofstream sFile(filename);
        serializeImp(p, sFile, buffer);
        sFile.close();
}

static bool getNextData(ifstream& inFile, char *buf)
{
        int i;
        char c;

        while(!inFile.eof() && (c = inFile.get()) == ' ');

        if(inFile.eof())
                return false;

        buf[0] = c;
        for(i = 1; !inFile.eof() && (c = inFile.get()) != ' '; i++)
                buf[i] = c;

        buf[i] = '\0';
        return true;
}

void deSerializeImp(btree **p, ifstream& inFile, int *val, char *buf, int min, int max)
{
        if(*val < min || *val > max)
                return;

        *p = createNode(*val);
        if(!getNextData(inFile, buf))
                return;
        *val = atoi(buf);

        deSerializeImp(&(*p)->left, inFile, val, buf, min, (*p)->data);
        deSerializeImp(&(*p)->right, inFile, val, buf, (*p)->data, max);
}

btree* deSerializeBST(char *filename)
{
        int val;
        btree *p = NULL;
        ifstream inFile(filename);
        char buffer[34];

        if(!getNextData(inFile, buffer))
                return NULL;
        val = atoi(buffer);

        deSerializeImp(&p, inFile, &val, buffer, INT_MIN, INT_MAX);
        inFile.close();
        return p;
}

int main(int argc, char *argv[])
{
        btree *head = NULL, *newTree = NULL;
        int arr[] = {30,20,10,40,35,50};
        if(argc != 2)
                cout << "invalid number of arguments" << endl;

        for(int i = 0; i < 6; i++)
                head = BinaryTree_insert(head, arr[i]);

        serializeBST(head, argv[1]);
        //newTree = deSerializeBST(argv[1]);
        return 0;
}

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

Use JSON :). It is very efficient. Just pre-order cannot construct a unique tree. So above solution is not correct. If you go that route you need in-order traversal as well which doubles up the storage space requirement.

- ashishkaila@hotmail.com November 21, 2011 | 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