Pergunta de entrevista da empresa Google

Write a function to caculate the angle between hour and minute hand on a clock.

Respostas da entrevista

Sigiloso

13 de jul. de 2011

Just few thoughts on interview process: - why the interviewer attacks that way at beginning? - when u know the answer, i think it's sort of honesty to say you had read this question before; then the interviewer might ask different ques, or he might ask to see how do u code it - finally, why do you said "Received and Declined Offer", when you failed 2nd phone screen. No offense, just ask for correction.

1

Sigiloso

13 de jul. de 2011

Quick answers 1. I have no idea why. 2. I didn't see the question before but I solved it too quickly and he accused me. 3. Oops, this needs to be corrected.

Sigiloso

14 de jul. de 2011

Oz, Your code has some bugs... the corrected code is below: public class Clock { public static void main(String[] args) { System.err.println(getAngle(Integer.parseInt(args[0]),Integer.parseInt(args[1]))); } public static double getAngle(int hours, int mins) { double angle = Math.abs((hours * (360.0/12.0)) - ((60 - mins) / 5.0 * (360.0/12.0))); return angle % 360.0; } }

Sigiloso

16 de dez. de 2011

Think of it this way: First, you need to take the hour and do a modulo 12 on it, because there aren't 24 hours on a clock. Next, every minute, the minute hand moves by 6 degrees (360/60). Now, the hour hand does a complete rotation in 12 hours, that would be 30 degrees per hour (360 degrees / 12 hours) Of course, as the minute hand rotates beyond H:00, the hour hand keeps advancing, so we need to account for that. We know it takes 60 minutes for the hour hand to advance by 30 degrees, so the correction is (fraction of an hour) * 30 degrees. So the angle between 12 o'clock and the position of the hour hand (Ha) is: Hour = Hour % 12 -> handle values past noon Ha = 30*Hour -> position of the hour hand without correction Ha = Ha + (Minute / 60 * 30 degrees) -> correction for the position of the hour hand And the angle between noon and the minute hand (Ma) is: Ma = 360 degrees / 60 minutes * Minute And so the angle between the two hands is: Abs(Ha - Ma) For instance, for 14h20: Hour = 14 % 12 = 2 Ha = 30 * 2 = 60 (uncorrected) Ha = Ha + (20 / 60 * 30) = 70° Ma = 360 / 60 * 20 = 120° Angle between the two = 50° Whenever you have a problem like this one, try to draw it on the board, and pick easy values, for instance 6:30, which is obviously not 0°, but half of the angle that represents 5 minutes, and you'll end up figuring it out pretty quickly.

Sigiloso

21 de mar. de 2012

function getAngle($h, $m) { $ha = $h * 30 + ((int) $m / 60) * 30; $ma = $m * 6; return abs($ma - $ha); }

Sigiloso

28 de abr. de 2013

Is this the question asked at Google for real? It's not hard at all.

Sigiloso

8 de jul. de 2011

Just make sure u take account of the angle of hour hand

Sigiloso

12 de jul. de 2011

public class Clock { /** * @param args */ public static void main(String[] args) { System.out.println(getAngle(2,11)); } public static double getAngle(int hours, int mins) { System.out.println(hours * 30.0 + 30.0*mins/60.0); System.out.println(mins*360/60); double angle = Math.abs((hours * 30 + 30.0*mins/60.0) - (mins*360.0/60.0)); return angle; } }

Sigiloso

13 de out. de 2011

Sorry Hasan, But I still don't think your code is correct. For example, if the time was 3:15, the angle should be 0 rather than 180 [ the output your code suggests. Similarly, if the time was 3:45, the angle should be 180 rather than 0 [ the output your code suggests. Why? because the question is the angle between the hour and the minute hand. I will post my solution soon. Its alot more complicated than people think!