About this question

Timespec Elapsed Nanoseconds (clock_gettime Arithmetic Pattern)

Easy · low_latency · Quant Developer interview question · clock-gettime, timespec, rdtsc, low_latency, timing, monotonic

High-frequency trading systems require nanosecond-precision timestamps to measure latency for market events. The clock_gettime function provides timestamps as a struct timespec, containing separate seconds and nanoseconds fields. Correctly calculating the elapsed time between two such timestamps requires careful mixed-unit arithmetic to avoid overflow or truncation errors. Task Implement the function elapsed_ns(long t1_sec, long t1_ns, long t2_sec, long t2_ns) to compute the total elapsed time