FastPrepOverlap Duration of Two Time Intervals

Overlap Duration of Two Time Intervals

Scale AI logoScale AI● EasyNEW GRADPHONE SCREEN
Learn

Problem statement

For this practice exercise, you receive two time intervals after their timestamps have been extracted and normalized: [startA,endA) and [startB,endB).

For this exercise, assume timestamps are integer minutes on the same timeline and both intervals are half-open: the start is included and the end is excluded. An interval with equal start and end is empty.

Return the elapsed number of minutes covered by both intervals. Disjoint, touching or empty intervals have overlap duration 0.

Interview context

The report describes two parts: extracting timestamp values from nested JSON with Python dictionaries and a supplied time-conversion helper, then calculating time overlap. The original JSON schema, number of intervals and output statistic are not preserved. This callable practice adapter receives four already-normalized endpoints and chooses the duration of their intersection. The author also recalls a possible extra field in the first part's output but forgot its identity; no such field is required here.

Function

overlapDuration(startA: int, endA: int, startB: int, endB: int) → int

Examples

Example 1

startA = 10endA = 20startB = 15endB = 30return = 5

The common interval is [15,20), lasting five minutes.

Example 2

startA = 5endA = 20startB = 8endB = 12return = 4

The second interval is contained in the first, so their common interval is [8,12).

Example 3

startA = 1endA = 4startB = 4endB = 9return = 0

The intervals touch at 4 but share no positive-duration time.

Constraints

  • For this exercise, assume 0 <= startA <= endA <= 1000000000 and 0 <= startB <= endB <= 1000000000.
  • The supplied integers are normalized elapsed minutes; no JSON parsing, timezone conversion or external request is required.
  • The returned duration lies in [0,1000000000] and fits a signed 32-bit integer.

More Scale AI problems

See Scale AI hiring insights
public int overlapDuration(int startA, int endA, int startB, int endB) {
    // write your code here
}
startA10
endA20
startB15
endB30
expected5
Checking account…