Convert a past timestamp into a human-readable time-ago string

Quick Overview

Write a function that turns the gap between two Unix timestamps into a relative time string such as 2 minutes ago or 1 day ago. It tests choosing the largest fitting unit, integer division at exact boundaries, and singular versus plural formatting.

Convert a past timestamp into a human-readable time-ago string

Company: Zoom

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Write a utility function that turns a timestamp into a human-readable relative time string. You are given two integers, `current_time` and `past_time`, both in seconds since the epoch. Return a string that says how long ago `past_time` was relative to `current_time`. If the difference is under 60 seconds, return `just now`. Otherwise return `<value> <unit> ago`, using the largest time unit that applies. Use the singular unit when the value is exactly 1 (`1 minute ago`) and the plural otherwise (`2 minutes ago`). ### Function Signature ```python def time_ago(current_time: int, past_time: int) -> str: ``` ### Rules Let `diff = current_time - past_time`. A minute is 60 seconds, an hour is 3,600, a day is 86,400, a month is 30 days (2,592,000 seconds) and a year is 365 days (31,536,000 seconds). The unit is chosen by the range `diff` falls in: | `diff` in seconds | Result | |---|---| | `diff < 60` | `just now` | | `60 <= diff < 3600` | minutes | | `3600 <= diff < 86400` | hours | | `86400 <= diff < 2592000` | days | | `2592000 <= diff < 31536000` | months | | `diff >= 31536000` | years | The value is the whole number of that unit, rounded down: `diff // unit_size`. Years start at 365 days, so a difference of 360 to 364 days still uses months and reads `12 months ago`. The output has a single space between the value, the unit and `ago`, all lowercase. ### Constraints - `0 <= past_time <= current_time <= 10^9` - `current_time - past_time <= 10^9` ### Examples ```text Input: current_time = 1000000, past_time = 999880 Output: "2 minutes ago" ``` The difference is 120 seconds. That is at least a minute and less than an hour, so the unit is minutes and the value is 120 // 60 = 2, which takes the plural. ```text Input: current_time = 1000000, past_time = 913600 Output: "1 day ago" ``` The difference is 86,400 seconds, exactly one day and less than 30 days. The value is 1, so the unit is singular. ```text Input: current_time = 31104000, past_time = 0 Output: "12 months ago" ``` The difference is 31,104,000 seconds, which is 360 days. That is below the 365-day year size, so the unit is months and the value is 31,104,000 // 2,592,000 = 12.

Overview: Write a function that turns the gap between two Unix timestamps into a relative time string such as 2 minutes ago or 1 day ago. It tests choosing the largest fitting unit, integer division at exact boundaries, and singular versus plural formatting.

Zoom logo
Zoom
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Write a utility function that turns a timestamp into a human-readable relative time string. You are given two integers, current_time and past_time, both in seconds since the epoch. Return a string that says how long ago past_time was relative to current_time.

If the difference is under 60 seconds, return just now. Otherwise return <value> <unit> ago, using the largest time unit that applies. Use the singular unit when the value is exactly 1 (1 minute ago) and the plural otherwise (2 minutes ago).

Function Signature

def time_ago(current_time: int, past_time: int) -> str:

Rules

Let diff = current_time - past_time. A minute is 60 seconds, an hour is 3,600, a day is 86,400, a month is 30 days (2,592,000 seconds) and a year is 365 days (31,536,000 seconds). The unit is chosen by the range diff falls in:

diff in secondsResult
diff < 60just now
60 <= diff < 3600minutes
3600 <= diff < 86400hours
86400 <= diff < 2592000days
2592000 <= diff < 31536000months
diff >= 31536000years

The value is the whole number of that unit, rounded down: diff // unit_size. Years start at 365 days, so a difference of 360 to 364 days still uses months and reads 12 months ago.

The output has a single space between the value, the unit and ago, all lowercase.

Constraints

  • 0 <= past_time <= current_time <= 10^9
  • current_time - past_time <= 10^9

Examples

Input: current_time = 1000000, past_time = 999880
Output: "2 minutes ago"

The difference is 120 seconds. That is at least a minute and less than an hour, so the unit is minutes and the value is 120 // 60 = 2, which takes the plural.

Input: current_time = 1000000, past_time = 913600
Output: "1 day ago"

The difference is 86,400 seconds, exactly one day and less than 30 days. The value is 1, so the unit is singular.

Input: current_time = 31104000, past_time = 0
Output: "12 months ago"

The difference is 31,104,000 seconds, which is 360 days. That is below the 365-day year size, so the unit is months and the value is 31,104,000 // 2,592,000 = 12.