← All topics/Performance & optimization

Practical interview questions

Scenario-style prompts with sample answer outlines. Focus is on how you would design and reason in real codebases.

Question 6

Table and collection view performance

A table or collection view gets sluggish as content grows. What mistakes do you look for, and how do you fix them?

Follow-ups

  • Reuse, Auto Layout, prefetch, diffable tradeoffs?

Answer outline

Four areas cause most table and collection view slowdowns:

  1. 1.ReuseprepareForReuse must reset all state; async loads must cancel and re-validate the item ID. Wrong reuse causes stutter and wrong content in cells.
  2. 2.Layout — too many constraints per cell, intrinsic content churn, or self-sizing without cached heights leads to repeated layout passes. Set an estimated row heigh and use the full Auto Layout chain for self-sizing cells.
  3. 3.PrefetchingUICollectionViewDataSourcePrefetching / UITableViewDataSourcePrefetching starts loads early. Cancel in cancelPrefetchingForItemsAt when the user scrolls away.
  4. 4.Diffable data source — requires stable IDs so the same item always has the same identifier. Gives animated updates with fewer performBatchUpdates bugs, but the model must be diff-friendly and snapshots must stay consistent.

Principles

  • Keep cellForItem cheap — move work to willDisplay or background tasks with a clear lifecycle.
  • Profile scrolling with Instruments: Core Animation FPS + Time Profiler together.
  • Avoid N × expensive Auto Layout in deep hierarchies — flatten or use manual layout for hot cells.
Estimated height (UIKit)
tableView.estimatedRowHeight = 120
tableView.rowHeight = UITableView.automaticDimension

Follow-up angles

  • SwiftUI List has the same identity rules — unstable ForEach IDs hurt scroll performance just as much.
  • Compositor-friendly cells use opaque backgrounds and fewer translucent overlays to reduce offscreen rendering.