Code RoomVector access before empty check
MediumPrep Room Coding #1694

Vector access before empty check

Code reviewCode quality & reviewMid–Senior~20 min

Review this C++ function that returns the first element of a vector or a default.

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 20 min
Mark a line and say what kind of problem it is.0 findings
1int firstOr(const std::vector<int>& v, int dflt) {
2 int first = v.front();
3 if (v.empty()) {
4 return dflt;
5 }
6 return first;
7}
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.