cybersecurity · intermediate · ~15 min · safe pentest lab

Frequency of the most common byte

Find the peak byte frequency — a flat histogram (low peak) signals high entropy.

Challenge

Complement the distinct-count signal with flatness:

int max_byte_frequency(const unsigned char *buf, size_t n);

Return the count of the single most frequent byte value. Encrypted data is flat (a low peak); plaintext spikes (spaces, e, ...).

Input format

buf of n bytes.

Output format

The count of the most frequent byte (0 if n==0).

Constraints

n==0 returns 0.

Starter code

#include <stddef.h>
/* Count of the MOST frequent byte value in buf (0 if n==0). Flat (low) => high entropy. */
int max_byte_frequency(const unsigned char *buf, size_t n){ (void)buf;(void)n; return 0; }

Common mistakes

Returning the byte value instead of its count; not handling the empty buffer.

Edge cases to handle

All-same returns n; a flat 256-value buffer returns n/256 rounded down per value.

Background lessons

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