This is another whiteboard exercise I was presented with in a real life interview. I had come across this several times in blogs and interview books but I still had to struggle when put on the spot. Solving this while sitting down at home is always going to be ten times easier than trying to think out loud in a roomful of strangers.

I started by misunderstanding the question and then over-thinking it; I was thinking mathematically because instead of being given a number of "rows" as a function parameter, it was presented as the "height". For this particular problem I quickly realized that I had to think in terms of a grid and allow for the spaces to the left of the X's - this was the crux of it. Otherwise I would have ended up with something like this:

X
XX
XXX
XXXX

The best advice I can give anyone in these types of situations is to get into a relaxed state. Think it out loud by asking yourself questions out loud and what it is you are going to actually try to do. Interviewers will then be tuned into your thinking and more able to offer hints as you move through it. Rationalize it in your own mind as you go along.

Here's a working example:

public static void CreateTriangle(int height)
{
    // Number of rows
    for (int i = 1; i <= height; i++)
    {
        // Print spaces
        for (int j =1; j <= (height - i); j++)
        {
            Console.Write(" ");
        }

        // Print X's
        for (int k = 1; k < (i * 2); k++)
        {
            Console.Write("X");
        }
        Console.WriteLine();
    }

    Console.ReadLine();
}

 




Disclaimer: I was recently asked this in an interview and quickly found myself dealing with some very messy indexing syntax using arrays. I decided to revisit the problem later to see if my initial hunch of treating the uint input as a string would tend toward a cleaner solution.

/// <summary>
/// Input treated as a string for simplification 
/// </summary>
/// <param name="x"></param>
/// <returns>bool</returns>
public static bool IsPalindrome(uint x)
{
    string givenNum = Convert.ToString(x);
    char[] input = givenNum.ToCharArray();    
    Array.Reverse(input);   

    string testString = String.Empty;
    foreach (char a in input)
        testString += a;

    return givenNum == testString;
}


It works fine but I still thought it was a little more verbose than it could be so I posted this question on Stack Overflow and got some very interesting responses. I eventually went with the following solution; since a uint was specifically specified as a parameter, it would seem that an interviewer would be looking for a numerical, non-string answer.

public static bool IsPalindrome(uint x)
{
    uint original = x;
    uint reverse = 0;

    while (x > 0)
    {
        reverse *= 10;
        reverse += x % 10;
        x /= 10;
    }
    return original == reverse;
}




When uploading a file, you will usually want to check that the extension is of the correct type:

if ((String.Compare(fileExt, ".pdf", true)) == 0) {...}

However, the above code will not work if the letters are in a different case.

This is a better way which ignores the case:

if ((fileExt.IndexOf("pdf", StringComparison.OrdinalIgnoreCase) == 0)) {...}

UPDATE

An even better way (counteracts input such as "pdf2", etc.)

if (string.Equals(fileExt, ".pdf", StringComparison.OrdinalIgnoreCase)) {...}