Katkat

Implement an iOS scrollable grid with navigation

SnapchatSoftware Engineer · Onsite

Last updated

This question tests the ability to build a responsive, performance-optimized grid interface in iOS using modern frameworks like SwiftUI, focusing on layout constraints, state management, and navigation flow.

Problem Statement

Design a scrollable 'Memory Grid' view for an iOS application. The interface should display a list of image items in a grid layout where each cell is a perfect square, regardless of the device screen width.

Requirements:

  1. Grid Layout: Create a scrollable view that displays items in 3 columns. The cells must remain square (width == height).
  2. Navigation: Tapping a cell should push a Detail View onto the navigation stack, showing the selected image.
  3. State Management: Implement a simple data model to store a list of image identifiers. Ensure the grid updates correctly if the underlying data changes.
  4. Performance: Use efficient list/grid rendering techniques suitable for large datasets (e.g., lazy loading).

Function Signature (SwiftUI approach):

struct MemoryGridView: View {
    let items: [String] // Array of image IDs
    
    var body: some View {
        // Implementation here
    }
}

Example

  • Input: items = ["img1", "img2", "img3", "img4"]
  • Layout: A 3-column grid where each cell occupies 1/3 of the screen width and maintains a height equal to its width.
  • Interaction: Selecting "img1" triggers NavigationLink to DetailView(imageID: "img1").

Constraints

  • The solution must support dynamic screen orientation changes (portrait to landscape).
  • The grid must be scrollable if the content exceeds the screen height.
  • Avoid hardcoding fixed pixel sizes for cells.

Hints

Hint 1

Consider using 'LazyVGrid' with a 'GridItem' configuration that uses 'flexible' spacing to distribute width dynamically.

Hint 2

To maintain square aspect ratios, use the 'aspectRatio(1, contentMode: .fit)' modifier on your cell view.

Hint 3

Wrap your main view in a 'NavigationStack' to enable programmatic or tap-based navigation transitions.

Follow-up questions

  • How would you optimize the scroll performance if you were loading high-resolution images from a remote URL?
  • How would you implement a 'pull-to-refresh' mechanism to fetch new images from a backend service?
  • If the grid required a 'Section Header' for each date, what modifications would you make to the layout data source?