0
0
Embedded Cprogramming~20 mins

Ring buffer for UART receive in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ring Buffer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this ring buffer read function?

Consider this simplified ring buffer read function for UART data. What will be the returned value when ring_buffer_read() is called?

Embedded C
typedef struct {
    unsigned char buffer[4];
    unsigned int head;
    unsigned int tail;
} RingBuffer;

RingBuffer rb = {{'A', 'B', 'C', 'D'}, 2, 0};

unsigned char ring_buffer_read(RingBuffer *rb) {
    if (rb->head == rb->tail) {
        return 0; // Buffer empty
    }
    unsigned char data = rb->buffer[rb->tail];
    rb->tail = (rb->tail + 1) % 4;
    return data;
}

int main() {
    unsigned char c = ring_buffer_read(&rb);
    printf("%c", c);
    return 0;
}
AA
BB
CC
DD
Attempts:
2 left
💡 Hint

Remember that tail points to the next byte to read.

Predict Output
intermediate
2:00remaining
What is the value of tail after reading one byte?

Given the same ring buffer as before, what is the value of rb.tail after calling ring_buffer_read(&rb) once?

Embedded C
typedef struct {
    unsigned char buffer[4];
    unsigned int head;
    unsigned int tail;
} RingBuffer;

RingBuffer rb = {{'A', 'B', 'C', 'D'}, 2, 0};

unsigned char ring_buffer_read(RingBuffer *rb) {
    if (rb->head == rb->tail) {
        return 0; // Buffer empty
    }
    unsigned char data = rb->buffer[rb->tail];
    rb->tail = (rb->tail + 1) % 4;
    return data;
}

int main() {
    ring_buffer_read(&rb);
    printf("%u", rb.tail);
    return 0;
}
A1
B2
C3
D0
Attempts:
2 left
💡 Hint

Tail moves forward by one position modulo buffer size.

Predict Output
advanced
2:00remaining
What happens if head equals tail in this ring buffer?

What will ring_buffer_read() return if rb.head equals rb.tail?

Embedded C
typedef struct {
    unsigned char buffer[4];
    unsigned int head;
    unsigned int tail;
} RingBuffer;

unsigned char ring_buffer_read(RingBuffer *rb) {
    if (rb->head == rb->tail) {
        return 0; // Buffer empty
    }
    unsigned char data = rb->buffer[rb->tail];
    rb->tail = (rb->tail + 1) % 4;
    return data;
}

int main() {
    RingBuffer rb = {{'A', 'B', 'C', 'D'}, 1, 1};
    unsigned char c = ring_buffer_read(&rb);
    printf("%d", c);
    return 0;
}
AA
B1
C0
DUndefined behavior
Attempts:
2 left
💡 Hint

When head equals tail, the buffer is empty.

Predict Output
advanced
2:00remaining
What is the output after writing and reading in this ring buffer?

Given this code that writes a byte then reads one, what is printed?

Embedded C
typedef struct {
    unsigned char buffer[4];
    unsigned int head;
    unsigned int tail;
} RingBuffer;

void ring_buffer_write(RingBuffer *rb, unsigned char data) {
    rb->buffer[rb->head] = data;
    rb->head = (rb->head + 1) % 4;
}

unsigned char ring_buffer_read(RingBuffer *rb) {
    if (rb->head == rb->tail) {
        return 0;
    }
    unsigned char data = rb->buffer[rb->tail];
    rb->tail = (rb->tail + 1) % 4;
    return data;
}

int main() {
    RingBuffer rb = {{0}, 0, 0};
    ring_buffer_write(&rb, 'X');
    unsigned char c = ring_buffer_read(&rb);
    printf("%c", c);
    return 0;
}
A0
BX
CA
DUndefined
Attempts:
2 left
💡 Hint

Write adds 'X' at head, then read returns from tail.

Predict Output
expert
2:00remaining
How many bytes can this ring buffer hold before head catches tail?

Given a ring buffer of size 4, what is the maximum number of bytes it can hold before it appears full (head catches tail)?

Embedded C
typedef struct {
    unsigned char buffer[4];
    unsigned int head;
    unsigned int tail;
} RingBuffer;
A1
B4
C2
D3
Attempts:
2 left
💡 Hint

One slot is left empty to distinguish full from empty.