Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

namespace T3
{
    //            Binary tree
    //          --------------
    //          |            |
    //      ---------    -----------
    //      |       |    |         |
    //     00      01   10      ---------
    //                          |       |
    //                        110      111
    // tree is uniquely defined by binary expressions {00, 01, 10, 110, 111}
    // we need to save integers {0, 1, 2, 6, 7} and bitlengths {2, 2, 2, 3, 3}
    // serizalization is compact presentation of these two arrays of integers
    // the bitlengths are bytes, the binary expressions usually are limited to 16 bits
    // and we use it as default.

    class Program
    {
        static byte[] SerializeTree(int[] expr, int[] leng)
        {
            int n = expr.Length * 3;
            byte[] serializedtree = new byte[n];
            for (int i=0; i<leng.Length; ++i)
            {
                serializedtree[i] = (byte)leng[i];
            }
            int counter = leng.Length;
            for (int i=0; i<expr.Length; ++i)
            {
                ushort sh = (ushort)expr[i];
                byte upper = (byte)(sh >> 8);
                byte lower = (byte)(sh & 0xff);
                serializedtree[counter] = upper;
                ++counter;
                serializedtree[counter] = lower;
                ++counter;
            }
            return serializedtree;
        }

        static bool DeserializeTree(byte[] data, out int[] expr, out int[] leng)
        {
            expr = null;
            leng = null;
            int m = data.Length;
            int n = m / 3;
            if (n * 3 != m) return false;
            expr = new int[n];
            leng = new int[n];
            for (int i=0; i<n; ++i)
            {
                leng[i] = data[i];
            }
            int counter = n;
            int position = 0;
            for (int i=0; i<n; ++i)
            {
                byte upper = data[counter];
                ++counter;
                byte lower = data[counter];
                ++counter;
                ushort sh = upper;
                sh <<= 8;
                sh |= lower;
                expr[position] = sh;
                ++position;
            }
            return true;
        }

        static void Main(string[] args)
        {
            int[] expr = { 0, 1, 2, 6, 7 };
            int[] leng = { 2, 2, 2, 3, 3 };
            byte[] result = SerializeTree(expr, leng);

            for (int i = 0; i < result.Length; ++i)
            {
                Console.Write(result[i]);
            }
            Console.WriteLine();

            int[] lengtest = null;
            int[] exprtest = null;

            if (!DeserializeTree(result, out exprtest, out lengtest))
            {
                Console.WriteLine("Deserialization failed");
            }
            else
            {
                for (int i = 0; i < lengtest.Length; ++i)
                {
                    Console.Write(lengtest[i]);
                }
                Console.WriteLine();

                for (int i = 0; i < exprtest.Length; ++i)
                {
                    Console.Write(exprtest[i]);
                }
                Console.WriteLine();
            }
        }
    }
}

- Student November 25, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Video on Serialize and Deserialize a Binary Tree in O(n) time and space complexity.
youtube.com/watch?v=jwzo6IsMAFQ

- Ven.w November 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Video on Serialize and Deserialize a Binary Tree in O(n) time and space complexity.
youtube.com/watch?v=jwzo6IsMAFQ

- Ven.w November 28, 2016 | 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