Please navigate to the bottom of the page for Table of Contents

Saturday, May 14, 2011

Simple coding questions–Part 1

During an interview (either in person or over phone), many interviewers like to ask very simple coding questions. This is usually to check how fast the interviewee can do a context switch in her mind, her ability to think about some very basic problem, etc.
So, let’s tackle a few of such coding problems in this post. I am using C# as the programming language in this example, but you can do the same in almost any modern language.

How to find the largest of 3 numbers

using System;

namespace SimpleCodingQuestions
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(FindLargestNumber(6, 3, 9));
        }

        /// <summary>
        /// Find the largest of three numbers
        /// </summary>
        /// <param name="a">first parameter</param>
        /// <param name="b">second parameter</param>
        /// <param name="c">third parameter</param>
        /// <returns></returns>
        public static long FindLargestNumber(long a, long b, long c)
        {
            // assume that the first value is the biggest
            long biggest = a;

            // check if b is the biggest
            if (b > biggest)
                biggest = b;

            // check if c is the biggest
            if (c > biggest)
                biggest = c;

            return biggest;
        }
    }
}



My next follow-up question is: Can you find the largest number in a variable length argument list? For those who come from the C/C++ background, this is the same as va_args list. In C#, this is referred by the the params keyword.

The params keyword lets you specify a method parameter that takes a variable number of arguments.

You can send a comma-separated list of arguments of the type specified in the parameter declaration, or an array of arguments of the specified type. You also can send no arguments.

No additional parameters are permitted after the params keyword in a method declaration, and only one params keyword is permitted in a method declaration.

For more details, refer to the MSDN article at http://msdn.microsoft.com/en-us/library/w5zay9db.aspx

Find the largest number in a variable length argument list

using System;

namespace SimpleCodingQuestions
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(FindLargestNumber(6, 3, 9));
            Console.WriteLine(FindLargestNumber(6));
            Console.WriteLine(FindLargestNumber(6, 3, 9, 2, 5, 8));
        }

        /// <summary>
        /// Find the largest number in a variable list 
        /// </summary>
        /// <param name="a">first parameter. Required.</param>
        /// <param name="numbers">variable list of longs</param>
        /// <returns></returns>
        public static long FindLargestNumber(long a, params long[] numbers)
        {
            // assume that the first value is the biggest
            long biggest = a;

            // loop through all the arguments passed
            foreach (long x in numbers)
            {
                // check if the current number is the biggest
                if (x > biggest)
                    biggest = x;
            }

            return biggest;
        }
    }
}

I hope this post helped you grasp an important concept. No question is simple. Even trivial questions can catch you off guard. Keep your focus razor sharp at all times. We will discuss a few more simple coding questions in the coming blog posts.

7 comments:

  1. Nice article , you have indeed cover the topic with great details. I have also blogged my experience as programming interview questions . let me know how do you find it.

    ReplyDelete
  2. Would it be silly to solve this in one line?

    using System;
    using System.Collections.Generic;
    using System.Linq;

    namespace ConsoleApplication15
    {
    public class Program
    {
    public static void Main( string[] args )
    {
    Console.WriteLine( "The largest number is {0}", new List { 5, 3, 9 }.Max() );
    }
    }
    }

    ReplyDelete
  3. Oops, the comment engine stripped out the less than int greater than that's part of the generic List. Let me try it again with an HTML encode.

    using System;
    using System.Collections.Generic;
    using System.Linq;

    namespace ConsoleApplication15
    {
    public class Program
    {
    public static void Main( string[] args )
    {
    Console.WriteLine( "The largest number is {0}", new List<int> { 5, 3, 9 }.Max() );
    }
    }
    }

    ReplyDelete