I'm trying to prepare for an interview and have looked at this problem:
There's a broken clock and a normal clock. The broken clock moves "rate" seconds each second, while the normal clock moves a standard one second per second. Both clocks start at 12:00:00, and AM/PM don't matter (as in, 01:23:15pm == 01:23:15am). Given "rate" and $X$ number of hours, how many times is the broken and normal clock at the same time over $X$ hours? "Rate" and $X$ can both be decimals, and "Rate" can also be negative.
I know how to solve this problem if "rate" and X are both integers (for each second, brokentime = (brokentime + rate) mod 43200 since there are 43200 seconds in a half day and AM/PM is not important), but how would I go about doing this if they were both decimals? I'm having trouble figuring out a formula.
We will take the case of seconds throughout the answer.
Let us understand what exactly 'rate' is. 'rate' is the time which is changed in the broken clock while it takes 1 second in the normal clock.
So let us define another variable difference. Difference is the difference in time shown by the clocks after 1 second when they both are set to the same time. Difference can be negative or positive. We can also define difference as $1 - r$ where $r$ is the rate.
For example, let in both the clocks the time be exactly 12 and rate of the broken clock be 2. $d= 1 - r = 1 - 2 = -1$ where $d$ is the difference. So after one second, the time in normal clock is $12:00:01$ while in the broken clock is $12:00:02$. After two seconds, the time will be $12:00:02$ in the normal one and $12:00:04$ in the broken one.
Now let $d_t$ be the difference in time of the two clocks after $t$ seconds.
Now a simple question. When are the two clocks equal? Simple answer. When the time they show is equal on a 12-hour clock. When is the time on a 12-hour clock equal? It is when $c_1 \equiv c_2 \pmod{43200}$ where $c_1$ and $c_2$ are the time shown by the two clocks.
So we want to find the length of the set $\{ d_t | d_t = 0 \text{ and }0 < t < X\cdot 60 \cdot 60\}$.
Also understand one thing. If the two clocks have the same time $n$ seconds from the start, they next time when they are equal will always be $n+n$ seconds.
Now let us find the time $t$ when $d_t = 0 \pmod{43200}$.
$$d_t = d \cdot t \implies d \cdot t = 0 \pmod{43200}$$
Now you have the value of $d$. Find the lowest positive $t$ which such that $d \cdot t$ is a multiple of $43200$. Now you have the value of $t$. What is left is easy. You have to find
$$\huge\lfloor\normalsize\frac{X \cdot 60 \cdot 60}{t}\huge\rfloor$$
The two 'brackets' on the side symbolize floor division. It means that you have to leave the fractional part of the answer and stick to the integer.
Example: When broken clock is literally broken and $r = 0$ and $X = 12$:
$$d = 1 \implies t = 43200 \implies \huge\lfloor\normalsize\frac{X \cdot 60 \cdot 60}{t}\huge\rfloor \normalsize = 1$$
which is true. You may verify it for other numbers.
Hope you found that useful.