Circular Buffer Push

Medium C Data Flow Asked at NXP
Solve this problem

Implement rb_push for a fixed-size circular buffer.

Return: * 1 on success * 0 if the buffer is full

This models how UART drivers store incoming bytes.

Input: A ring buffer and a value to push. Output: 1 if inserted, 0 if full.

Constraints

  • No dynamic memory.
  • Constant-time operation.
  • Do not overwrite existing data.
Starter codeC
1#define SIZE 42 3typedef struct {4    int data[SIZE];5    int head;6    int count;7} Ring;8 9int rb_push(Ring *r, int v) {10    // Implement11    return 0;12}