Thursday, July 21, 2011

.Net technical test drawing mountains

Recently, my friend was given a technical test during an interview, i realized that it's a rather common "test" given to interviewees. I took up the challenge and squeeze out some time to do it.

The interviewer asked my friend to write a console application that reads from user's keyboard input, when the user enters a number, draw a mountain with the user input as the number of back and forward slashes. (of course, with bigger numbers, the subsequent layers need more padding between the slashes).

I wasn't exactly sure how the question was structured, but based on my understanding, i scribbled typed the expected end results on a text editor.


/\         gap=0, space=0, input=1

 /\        gap=1, space=0, input=2 (print new line)
/  \       gap=0, space=2

  /\       gap=2, space=0, input=3 (print new line)
 /  \      gap=1, space=2 (print new line)
/    \     gap=0, space=4 (print new line)

   /\      gap=3, space=0, input=4
  /  \     gap=2, space=2 (print new line)
 /    \    gap=1, space=4 (print new line)
/      \   gap=0, space=6 (print new line)

So, with my iPad showing me the expected results and my primary monitor with visual studio 2010... i started writing some loops:



using System;


namespace DrawMountains
{
    class Program
    {
        static void Main(string[] args)
        {
            int inputNum; 
            if (int.TryParse(Console.ReadLine(), out inputNum))
            {
                for (int i = 0; i < inputNum; i++)//print tip of mountain & padding (if any) 
                {
                    int temp = inputNum-i; //amount of padding to print
                    while (temp > 1) //amount of space before left slope
                    {
                        PrintSpace();
                        temp--;
                    }
                    PrintLeftSlope();
                    //----------------prints all left slope correctly (up to n num)
                    int temp2 = i*2;
                    for (int j = 0; j<temp2; j++) //padding between the slashes
                    {
                        PrintSpace();
                    }
                    PrintRightSlope(); //self-explanatory
                }
                Console.ReadKey();
            }
            else
            {
                Console.WriteLine("Invalid input");
                Console.ReadKey();
            }
        }


        static void PrintLeftSlope()
        {
            Console.Write("/");
        }


        static void PrintRightSlope()
        {
            Console.WriteLine(@"\");
        }


        static void PrintSpace()
        {
            Console.Write(" ");
        }
    }
}

This post is purely for educational purposes, no point for memorizing this sample and contrived effort. I believe the interviewer's purpose is to see how strong are you in the way you design your algorithms, handling exception (which i didn't do) and perhaps your naming convention and formatting. 

No comments:

Post a Comment