So, in my quest to become gainfully employed, I am going through the interview process, mainly technical interviews with hands-on developers. Unfortunately, they seem to want people that 1) either *just* graduated from Computer Science school, or 2) can memorize obscure bits of code, and recite the Visual Studio help files verbatim.
At my last interview, I walked in – after a two-hour car ride, and trying to find a parking spot – to a “Hi, let’s write some code on the white board” situation. Ok, I say to myself, “let’s give this a try”. Their first question, “write code to calculate the optimum number of coins to return when giving change after a purchase.”
Hmm, I think. And I stumbled around for a bit, eventually ending up writing some pseudo-code that involved some long division. The Inland Empire .NET User’s Group’s next meeting was the following Tuesday, and I decided to ask them the same question – however they had the luxury of being in a friendly environment with soda and pizza in their fiery little bellies.
Below are the two answers that UG members sent to me, and it got me to thinking. What I would like to do is have you write your code in the comments, so I can see how many different ways there are to write this method.
From member Daniel Lopez, “Just for grins. If you have suggestion on how to make it better, let me know.”
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GiveChange
{
class Program
{
static void Main(string[] args)
{
DoChange(8.92);
}
private static void DoChange(double Tendered)
{
double _DecimalPart = Math.Round(1-( Tendered - Math.Floor(Tendered)),2);
do
{
if (_DecimalPart >= .25)
{
Change.Quarters += 1;
_DecimalPart -= .25;
}
else if (_DecimalPart >= .10)
{
Change.Dines += 1;
_DecimalPart -= .1;
}
else if (_DecimalPart >= .05)
{
Change.Nickles += 1;
_DecimalPart -= .05;
}
else
{
Change.Pennies += (int)(_DecimalPart * 100);
_DecimalPart -= _DecimalPart;
}
}
while (_DecimalPart > 0);
StringBuilder sb = new StringBuilder();
sb.Append(string.Format( "Quarters: {0}", Change.Quarters));
sb.AppendLine();
sb.Append(string.Format("Dines: {0}", Change.Dines));
sb.AppendLine();
sb.Append(string.Format("Nickles: {0}", Change.Nickles));
sb.AppendLine();
sb.Append(string.Format("Pennies: {0}", Change.Pennies));
Console.WriteLine(sb.ToString());
Console.ReadLine();
}
}
public static class Change
{
public static int Quarters { get; set; }
public static int Dines { get; set; }
public static int Nickles { get; set; }
public static int Pennies { get; set; }
}
}
And from member Henry Vander Leest, "Hi James, Was thinking about the comments you made at the meeting about the interview you had the the question about the code to make change. Well, here's my solution... I don't believe I could have written this in 5 minutes under stressful circumstances. Live Long and prosper."
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ChangeMaker1
{
class Program
{
static void Main(string[] args)
{
//takes command line arg from 1 to 99 to make change for a dollar
int quarters;
int dimes;
int nickels;
int pennies;
int tendered = 100;
int cost = Convert.ToInt32(args[0]);
int change = tendered - cost;
quarters = change / 25;
change -= quarters * 25;
dimes = change / 10;
change -= 10 * dimes;
nickels = change / 5;
change -= nickels * 5;
pennies = change;
Console.WriteLine("Quarters {0}, Dimes {1}, Nickels {2}, Pennies {3},
This is the change for item costing {4} cents",
quarters, dimes, nickels, pennies, cost );
}
}
}
I tested both, and they work like a champ. Now I just need to memorize these for my next round of interviews. So tell me, how would you do this “simple” exercise.
Time to work…
James