Your firmware is close to the flash and RAM limits of the MCU. How do you reduce memory usage?
What the interviewer wants to hear
- Measure first with the map file, size and nm
- Compiler flags: -Os, LTO, function/data sections with gc-sections
- Remove heavy library features like float printf
- Make tables const so they stay in flash
- Right-size stacks, buffers and heap using measured usage
Model answer
I start by measuring, not guessing: the linker map file and arm-none-eabi-nm --size-sort show which functions and data are largest, and tools like puncover make it visual. For flash, I build with -Os, enable link-time optimisation, and use -ffunction-sections and -fdata-sections with --gc-sections so unused code is dropped. Library choices often matter most: a printf with float support or a big HAL driver can cost tens of kilobytes, so I use newlib-nano or a small printf, and register-level code for simple peripherals. For RAM, I make lookup tables and strings const so they stay in flash, measure task stacks with high-water marks and shrink them, size buffers from real data rates, and remove the heap if nothing needs it. I track memory size in CI so growth is caught in code review.
Can you answer it under time pressure?
Take a timed medium round and get scored on every answer.
More medium Embedded Systems Engineer questions
- How would you write a driver for a new I2C temperature sensor?
- Explain the difference between a mutex and a semaphore in an RTOS, with examples.
- A device freezes randomly once every few days in the field. How do you debug it?
- Explain the memory layout of a typical microcontroller program: .text, .data, .bss, stack and heap.
- When would you use fixed-point arithmetic instead of floating point on a microcontroller?
- What is DMA, when would you use it, and what pitfalls do you watch for?
- Design firmware that receives text commands over UART, like "SET LED 1", and responds reliably.
- Explain how the CAN bus works and why it is popular in automotive and industrial systems.
- How would you design a watchdog strategy for firmware running several RTOS tasks?