skip to content

In an offline language-learning app, lesson packs carry about 60 MB of audio each; how should the app store them and handle a nearly full phone?

level: seniorimportance: nice to knowfreq 36%

answer

  1. bytes in files, facts in rows
  2. not the purgeable folder
  3. temporary file, verify, then mark ready
  4. reconcile rows and files at launch
  5. space is shared with everything else

basics

~20 s

Keep audio as files in the app's persistent private storage with a database row per pack, mark a pack ready only after its file is complete, check free space before downloading, and let the learner see and remove downloads.

solid answer

~40 s

Audio goes in files, not database rows, in the app's private persistent storage rather than the OS's purgeable cache folder, because the learner downloaded it to use offline. A row per pack records its state (queued, downloading, ready, failed), file path, byte size and checksum, and screens query the rows. The download writes to a temporary file, verifies the checksum, moves it into place and only then marks the row ready, so a kill midway never leaves a ready pack with half a file. At launch I reconcile rows against files. Before starting I compare the pack size with the free space the platform reports, treat a full disk mid-write as an expected error, and show storage per pack so the learner can remove some.

code

pseudocode · 22 lines
pseudocode
download_pack(pack):
  if free_space_for_app() < pack.size_bytes + safety_margin:
    set pack.state = 'failed', pack.failure_reason = 'no_space'
    return                                    -- the storage screen explains what to remove
  set pack.state = 'downloading'
  temp_file = temporary_path(pack.id)
  result = stream(pack.url, into = temp_file)
  if result is error:                         -- includes a full disk mid-write
    delete temp_file
    set pack.state = 'failed', pack.failure_reason = result.reason
    return
  if size(temp_file) != pack.size_bytes or checksum(temp_file) != pack.checksum:
    delete temp_file
    set pack.state = 'failed', pack.failure_reason = 'corrupt'
    return
  move temp_file to final_path(pack.id)
  set pack.state = 'ready', pack.path = final_path(pack.id)

reconcile_at_launch():
  for each pack with state 'downloading': set state = 'failed'
  for each pack with state 'ready' whose file is missing: set state = 'not_downloaded'
  for each file in the packs folder without a 'ready' row: verify and adopt it, or delete it

go deeper

for a junior

Recall that large media belongs in files with a database row describing each one, and that the cache folder is not for downloads the user asked to keep.

for a middle

Explain the download sequence of temporary file, verification, move and then the ready state, and why the row never claims more than the file holds.

for a senior

Handle the failure cases: a kill between steps, a full disk mid-write, a missing file at launch, and give the learner a clear storage screen.

for a principal

Set a download and retention policy for offline media across products, balancing offline promises, device space and support cost from failed downloads.

Offline media is where the store choice meets the file system. The language-learning app lets learners download lesson packs, each with roughly 60 MB of audio (the scenario's own figure). Forty packs would be 2,400 MB; with 1 GB free, fewer than 17 packs fit, and fewer still once a safety margin is kept, so the app should say so before the learner queues forty. The interviewer wants to hear where the bytes go, how the app knows a pack is usable, and what happens when space runs out. ## Why files, and why not the cache folder - **Files, not blobs.** A 60 MB value inside a database row bloats the database file that every query, backup and migration touches, and many embedded databases return a blob by reading it into memory. Audio is streamed from a file by the player; the database keeps the facts about it. - **Persistent storage, not the cache folder.** Both native platforms give each app a cache folder that the OS may delete to recover space when storage is low, without saying when. The learner downloaded these packs so they would be there offline, so they belong in the app's private persistent storage. - **Backup.** On one platform the guidance asks apps to exclude files that can be downloaded again from device backup, to keep backups small; the packs qualify. The learner's review history, which cannot be downloaded again, does not, and stays in the backed-up store. ## The row that points at the file Each pack has one row in the database. The screens never inspect the folder; they observe these rows. | Column | Purpose | |---|---| | `pack_id` | Stable id of the pack | | `state` | `queued`, `downloading`, `ready`, `failed` or `not_downloaded` | | `path` | Where the finished file lives | | `size_bytes`, `checksum` | Expected size and content hash from the server | | `failure_reason` | For example `no_space`, shown to the learner | ## Keeping the pair consistent The file system is not part of the database's transaction, so a kill can always separate a file from its row. The design makes every split safe: 1. Mark the row `downloading` and stream the bytes into a **temporary file**. 2. When the stream ends, **verify** the size and checksum; on a mismatch delete the temporary file and mark the row `failed`. 3. **Move** the verified file to its final path; a move within the same storage area is the usual way to make a file appear complete in one step. 4. Only then mark the row **`ready`**. At launch, a **reconcile** pass handles whatever a kill left behind: rows stuck in `downloading` go back to `failed` or resume, a `ready` row whose file is missing goes back to `not_downloaded`, and a file with no `ready` row is either verified and adopted or deleted. Each outcome costs at most a re-download, never a broken lesson. ## Space is the device's, shared with every other app - **Check before starting.** One platform's storage guidance tells apps to query free space before writing app files; compare the pack's size plus a safety margin with what the platform reports as available. - **Expect a full disk anyway.** Free space can vanish while a long download runs. Treat the write error as a normal outcome: delete the partial file, mark the row `failed` with `no_space`, keep every `ready` pack intact and never retry in a loop. - **Make storage visible.** Show the size of each downloaded pack and the total, and let the learner remove packs; offer to remove packs for finished courses. - **Prefer removing finished content.** Before suggesting the learner clear space elsewhere, offer to remove packs for courses they have completed, which the app can download again if needed. - **Account for the store too.** The database itself grows with review history; it competes for the same space and should be included in the storage screen's total. ## Mistakes interviewers listen for - Writing straight to the final path and marking `ready` when the request starts. - Assuming a database row and its file change atomically together. - Treating a full disk as impossible, so a failed write leaves a half file that later fails to play. - Storing packs in the purgeable cache folder because they could be downloaded again.

  • Why not store the audio as blobs inside the database?
    A 60 MB value bloats the database file that every query, backup and migration touches, many embedded databases read a blob into memory to return it, and a player streams most simply from a file. Bytes in files and facts in rows let screens query packs cheaply while the player opens the file directly.
  • What should happen when the phone runs out of space halfway through a download?
    Treat it as an expected outcome: delete the partial temporary file, mark the pack failed with a no-space reason, leave every ready pack untouched, and tell the learner how much space is needed with a way to remove other packs. Never retry in a loop against a full disk.

  • Downloaded lesson audio can go in the OS's cache folder because it can be fetched again.
  • Marking a pack ready when its download starts is fine, since downloads usually succeed.
  • A database row and the file it points at are updated atomically by the same transaction.
  • A full disk is too rare for the download code to handle write errors.
  • Each app has a fixed storage quota reserved for it, so free space never needs checking.