cybersecurity · intermediate · ~15 min · safe pentest lab

Dominant beacon interval

Find the most common gap between timestamps — the likely beacon period.

Challenge

Even with noise, the beacon period is usually the most common gap:

int dominant_interval(const int *ts, int n);

Return the most frequent consecutive interval (the mode of the n-1 gaps). On a tie, return the smallest such interval. Return -1 if n<2.

Input format

ts sorted ascending, count n.

Output format

The modal interval, or -1.

Constraints

n<2 returns -1; ties resolve to the smallest interval.

Starter code

#include <stddef.h>
/* ts sorted ascending, n timestamps. Return the most common consecutive interval
   (mode of the n-1 gaps); on ties return the smallest such interval. -1 if n<2. */
int dominant_interval(const int *ts, int n){ (void)ts;(void)n; return -1; }

Common mistakes

Not handling ties deterministically; returning an index instead of the interval value.

Edge cases to handle

All gaps distinct: any gap is a valid mode (count 1) — the smallest wins.

Background lessons

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.