One of the trickiest parts I imagine with hiring a new developer is figuring it out several things about them. One thing you need to figure out is wether they will fit. What I mean is, even the best developer in the world won’t increase a team’s productivity if they don’t mesh well with that team. This means avoiding people that are more likely to force their programming viewpoints on others, or abuse their repo access to “refactor” other peoples code for no distinguishable reason. Additionally, and most obvious- is whether they can actually write code. An astonishing number of applicants to developer positions could not write trivial code in the language they were supposed to know.
The former is why personal interviews- or, if nothing else is possible, a voice communication or phone interview- are used. Though in-person interviews also help determine their personality based on their presentation. The latter- determining whether they are really a developer- is why you often have coding interview questions. I thought I’d look some up and explore them. Why? I dunno.
1.
Given that Pi can be estimated using the function 4 * (1 – 1/3 + 1/5 – 1/7 + …) with more terms giving greater accuracy, write a function that calculates Pi to an accuracy of 5 decimal places.
Now, some developers might end up with something like this:
1 2 3 4 5 6 7 8 9 10 11 |
public static double CalcPI() { float accuracy = 0.000001f; float buildit = 1; int multiplicand = 1; for (int currdivisor = 3; (1f / (float)currdivisor) > accuracy; currdivisor += 2) { buildit += ((multiplicand *= -1) * (1f / (float)currdivisor)); } return 4f * buildit; } |
I rather like the use of the *= directly in an expression. Anyway, this is sort of overthinking it. Look at the question. Like a proof, it provides a given- but, you don’t have to use it Since Pi is a constant, you could just do this:
1 |
public static double CalcPI() { return 3.14159;} |
It does what the function wants- it returns Pi to 5 decimal places. However, it also sort of defies the spirit of asking the question; the purpose is to see if you can use loops and basic control structures. This is more a early screen phase, so those looking to hire can avoid the people who thought “C# Developer” in the ad meant somebody who had expertise building Musical instruments. Even my implementation could easily be improved in any number of ways (not using a int as the for loop variable being one example). But it get’s the point across.
The Clock Angle Problem
Another common question is a tad more involved:
1 |
Write a procedure that, given the time of day, will compute the angle (positive) between the minute hand and the hour hand. |
Alright, now we get to some mild brainwork here.It’s interesting because it promotes a bit of thought about the problem. First, you mgiht start by figuring you can just get the angle for the Hour, the angle for the minute, and take the difference:
1 2 3 4 |
public static double getAngle(DateTime fortime) { return Math.Abs((((float)fortime.Hours%12)/12f)-((float)fortime.Minutes/60f))*Math.PI*2); } |
However, it is not that simple. Consider the following-
1. Look at an analog clock. When it is 9:30, for example, the hour hand is not pointing at the 9. It’s pointing halfway between 9 and 10. So we need to add an angle equal to the percentage the Minute hand is around a full revolution of the distance between two hours.
2. The difference between the 59’th minute and 12AM or 12PM is not one degree from a full revolution as this would show.
The first problem is “solved” by adding the appropriate amount. The second requires a bit of finagling. The final result for me was this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
static double getShortAngle(double a1,double a2) { var angle = (Math.Abs(a1 - a2))%360; if(angle > 180) angle = 360 - angle; return angle; } static double CalcAngle(DateTime timetest) { //Write a procedure that, given the time of day, //will compute the angle (positive) between the minute hand and the hour hand. //First get the minute hand angle. double MinuteAngle = (2*Math.PI)*(float)timetest.Minute / 60; //now we need the hour Angle. One common 'mistake' here is to forget that //the hour hand moves between the numbers smoothly, so the angle will also //depend on the minute. //first, we get the angle between any two numbers. this is single, a full revolution divided into 12. double hourincrement = ((Math.PI * 2) / 12); //this is the angle between the hours. //hour angle is hour increment multiplied by the hour plus //we need to also modulus the hour int currhour = timetest.Hour % 12; return getShortAngle((Math.PI * ((float)currhour / 12)) + (((float)timetest.Minute / 60) * hourincrement), MinuteAngle); } |
You can see my comments in there as well; I wrote those before I added the code which they “document”. Now, even this could be argued as being wrong in much the same way as the original version was wrong; the minute hand, like the hour hand, does not “jump” between each minute (depending on the clock); instead the minute hand moves smoothly between the minute markings, based on the second. So in order to be “complete” we’d need to also add in that. But this is much more accurate. The Angle check is perhaps the least obvious part, but is relatively simple and doesn’t require any super advanced trigonometry either.
Have something to say about this post? Comment!