Code RoomCounter loses increments
HardPrep Room Coding #2235

Counter loses increments

Code reviewStorage & CDNConcurrencySenior–Staff~22 min

Review this Python counter persisted to a shared file.

What a strong answer looks like

Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix, specific and kind.

0:00 of about 22 min
Mark a line and say what kind of problem it is.0 findings
1import json, os
2 
3COUNTER_FILE = "/var/lib/app/counter.json"
4 
5def increment_counter():
6 if os.path.exists(COUNTER_FILE):
7 with open(COUNTER_FILE) as f:
8 state = json.load(f)
9 else:
10 state = {"count": 0}
11 state["count"] += 1
12 with open(COUNTER_FILE, "w") as f:
13 json.dump(state, f)
14 return state["count"]
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.