All jobs / Backend developer / Backend Languages
Backend developer interview question · 0–1 years
When do you use an array rather than a List<T>?
The strongest answer
When the size is fixed and known, or an API demands one — List<T> grows, exposes Add and Remove, and is what most code should take and return
Why it works
Right. Fixed size is the reason; otherwise List<T> says more about intent.
What a good answer covers
- Array = fixed size.
- List<T> = grows, richer API.
- Accept IEnumerable<T>, return a concrete type.
- Span<T> for slicing without copying.
Answers that fall short
- Close, but not the strongest: Always; arrays are faster so they are the right default
The difference rarely matters next to the readability. - Weak: Never; arrays are legacy
They are still the natural fit for a fixed-size buffer. - Red flag: When the elements are value types
Element type does not decide it.
Who gets asked this
More Backend Languages questions
- You want a value from a dict, or a default if the key is missing, without raising. What do you use?
- You remove items from a List<T> inside a foreach over it and get an exception. Why?
- if items: — what exactly is being tested?
- When is var a good idea?
- Building a long string in a loop with s += piece is slow on big inputs. Why?
- Why expose a property rather than a public field?
- What does xs[1:4] give you, and what does xs[:] do?
- const versus readonly — what is the practical difference?