~/coding-interview/given-time-calculate-angle-between-hour-minute.md

How to calculate angle between hour and minute hand, given a time

2 min readAugust 31, 2019by Gorav Singal#cracking-the-coding-interview#coding-problems#algorithm
// TL;DR

The minute hand moves 6 degrees per minute, the hour hand moves 0.5 degrees per minute. Compute both angles from 12 o'clock, take the absolute difference, and return min(diff, 360-diff).

How to calculate angle between hour and minute hand, given a time

This problem is a simple mathematical calculation. Lets start deriving some equations. Note: Here we are talking about the round clock, which displays 1-12 hours.

Angle of Minute hand at a particular time

A complete round of a clock is equal to 360 degree angle. This is simple, minute hand complete 360 degree in 60 minutes. Or, it takes 60 minutes to complete 360 degree

60 M -> 360 degree
1 M -> 360/60 -> 6 degree

Means, minute hands moves 6 degree per minute.

For 'M' minutes, it will be 6 * M degree

Angle of Hour hand at a particular time

Hour hand takes 12 hours to complete a round i.e. 360 degree. But, if you notice. When hour hand moves, there will be a movement in minute hand too, due to that hour hand will move to some extent.

For hour hand:
12 Hours -> 360 degree
1 Hour -> 360/12 -> 30 degree

For 'H' Hours -> 30 * H degree

And, there will be some movement in minute hand too. Continuing above equation:
12 Hours -> 360 degree
12 * 60 Minutes -> 360 degree
1 Minute -> 360 / (12 * 60) -> 1/2 degree

In 'M' minuets -> M/2 degree

Combining them, Angle of hour : 30 H + M/2

Difference of Angle between Hour and Minute hand

As from above equations:

Angle of Hour hand - Angle of Minute hand
=> (30 H + M/2) - (6 M)
=> 30 H - 5.5 M

Example

Angle at time: 6:15

30 H - 5.5 M
=> 30 * 6 - 5.5 * 15
=> 180 - 82.5
=> 97.5

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

Related Posts

Determine if a string has all unique characters

Determine if a string has all unique characters

Problem Implement an algorithm to determine if a string has all the characters…

How to calculate First Common Ancestor of two Nodes in Binary Tree

How to calculate First Common Ancestor of two Nodes in Binary Tree

First try to understand question. Its a binary tree, not a binary search tree…

Leetcode Solution - Best Time to Buy and Sell Stock

Leetcode Solution - Best Time to Buy and Sell Stock

Problem Statement You are given an array prices where prices[i] is the price of…

Binary Tree - Level Order Traversal

Binary Tree - Level Order Traversal

Problem Statement Given a Binary tree, print out nodes in level order traversal…

Four Sum - Leet Code Solution

Four Sum - Leet Code Solution

Problem Statement Given an array nums of n integers and an integer target, are…

Leetcode - Rearrange Spaces Between Words

Leetcode - Rearrange Spaces Between Words

Problem Statement You are given a string text of words that are placed among…

Latest Posts

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…