Coding Interview Questions

Jan 24th, 2013
Coding Interview Questions

This is from a series of blog posts that Nikhil has written on his blog about cracking programming interviews. We'll be posting some of them from time to time to keep our new users informed and others refreshed.

Here are a few basic coding problems. We use C# as the programming language in this example, but you can do this in any language.

How do you find the largest of 3 numbers given to you

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;
        }
    }
}




The next question is: How do you find the largest number in a variable length argument list? For C/C++ folks, this is like va_args list. In C#, this goes by the params keyword.

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

You can also 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 http://msdn.microsoft.com/en-us/library/w5zay9db.aspx

 

How would you 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;
        }
    }
}

Moral: No question is simple. Even easy questions can trip you up. Pay attention!

 

More beginner programming questions (follow the link to the question and answers)

How to find if a number is a Palindrome?

Linked lists demystified

Recursion–concepts and code

Linked Lists – Sorting, Searching, Finding Maximum and Minimum

Reverse a Linked List

Searching algorithms–Linear search

Binary Search

String manipulation can be fun

String pattern matching

String functions galore

Pass by value versus reference in C#

How to find if a number is perfect square

 

About the author:

Nikhil Singhal is a engineering director and architect with over 15 years of experience in large scale enterprise, web, and mobile products utilizing .NET, JAVA and open source stacks. He blogs on how to do well in programming interviews at http://www.ProgrammingInterviews.info

latest articles