Fix the save model for a form wizard
Your task
An AI wrote a React Native form wizard that saves progress to AsyncStorage, so answers survive if Android kills the app. Users still lost answers. The save ran right after setData, while the callback still held the previous render's data, so each save was one update behind. The restore also skipped JSON.parse, so the form got a string instead of an object. Your team models save and restore to see what each version brings back.
An assistant wrote restore_wizard_answers(update_lines, save_after_update, parse_on_restore), the model. It is in the editor. It has a bug. Find it and fix it.
Input and output:
update_lines: the user's answers in order, each writtenkey=value.save_after_update: true for the fixed save, which stores the answers after each update. False for the original save, which stores the answers from just before each update.parse_on_restore: true when the restore parses the saved JSON.- Return the restored answers as
key=valuestrings, sorted by key.
Rules the tests check:
- Split a line at its first
=. Skip it if there is no=or the key is empty. A skipped line changes nothing and saves nothing. - Each kept line sets its key to its value. Setting a key again overwrites it.
- Each kept line also saves a snapshot: the answers after the line when
save_after_updateis true, or the answers from just before it when false. A snapshot never changes once taken. - The restore uses the last snapshot. Return an empty list if
parse_on_restoreis false or nothing was saved. - Sort the output by key, using plain string comparison.
Examples
restore_wizard_answers(update_lines: list[str], save_after_update: bool, parse_on_restore: bool) -> list[str]Example 1
Input
restore_wizard_answers(["name=Ada", "email=ada@x.io", "role=eng"], True, True)Output
["email=ada@x.io", "name=Ada", "role=eng"]Example 2
Input
restore_wizard_answers(["name=Ada", "email=ada@x.io", "role=eng"], False, True)Output
["email=ada@x.io", "name=Ada"]Example 3
Input
restore_wizard_answers(["name=Ada", "email=ada@x.io"], True, False)Output
[]