0
0
Embedded Cprogramming~20 mins

Ring buffer implementation in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ring Buffer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of ring buffer enqueue and dequeue operations

Consider a ring buffer of size 4. After enqueuing 3 elements and dequeuing 1, what is the value returned by the next dequeue?

Embedded C
typedef struct {
    int buffer[4];
    int head;
    int tail;
    int max;
    int count;
} RingBuffer;

void enqueue(RingBuffer *rb, int val) {
    if (rb->count == rb->max) return; // full
    rb->buffer[rb->head] = val;
    rb->head = (rb->head + 1) % rb->max;
    rb->count++;
}

int dequeue(RingBuffer *rb) {
    if (rb->count == 0) return -1; // empty
    int val = rb->buffer[rb->tail];
    rb->tail = (rb->tail + 1) % rb->max;
    rb->count--;
    return val;
}

int main() {
    RingBuffer rb = {.head=0, .tail=0, .max=4, .count=0};
    enqueue(&rb, 10);
    enqueue(&rb, 20);
    enqueue(&rb, 30);
    dequeue(&rb);
    int result = dequeue(&rb);
    return result;
}
A20
B10
C30
D-1
Attempts:
2 left
💡 Hint

Remember that dequeue removes the oldest element first.

🧠 Conceptual
intermediate
1:30remaining
Ring buffer full condition check

Which condition correctly checks if a ring buffer is full?

Arb->count == rb->max
Brb->head == rb->tail
C(rb->head + 1) % rb->max == rb->tail
Drb->count == 0
Attempts:
2 left
💡 Hint

Count tracks how many elements are currently stored.

🔧 Debug
advanced
2:00remaining
Identify the bug in ring buffer enqueue function

What is the bug in this enqueue function for a ring buffer?

Embedded C
void enqueue(RingBuffer *rb, int val) {
    if (rb->count == rb->max) return;
    rb->buffer[rb->head] = val;
    rb->head = (rb->head + 1) % rb->max;
    rb->count++;
}
AThe buffer array is accessed out of bounds
BThe condition should be rb->count == rb->max, not >
CThe count should be decremented, not incremented
DThe head index should not wrap using modulo
Attempts:
2 left
💡 Hint

Check the condition that prevents adding when full.

📝 Syntax
advanced
1:30remaining
Syntax error in ring buffer initialization

Which option correctly initializes a ring buffer struct in C?

ARingBuffer rb = {head:0, tail:0, max:8, count:0};
BRingBuffer rb = [0, 0, 8, 0];
CRingBuffer rb = (head=0, tail=0, max:8, count=0);
DRingBuffer rb = {.head=0, .tail=0, .max=8, .count=0};
Attempts:
2 left
💡 Hint

Use designated initializers with dot notation in C.

🚀 Application
expert
2:30remaining
Calculate number of items after mixed operations

Given a ring buffer of max size 5, starting empty, after these operations:

  1. enqueue 1
  2. enqueue 2
  3. dequeue
  4. enqueue 3
  5. enqueue 4
  6. enqueue 5
  7. dequeue
  8. enqueue 6

How many items are currently stored in the buffer?

A3
B5
C4
D2
Attempts:
2 left
💡 Hint

Track count carefully after each enqueue and dequeue.