Mean Front-to-Rear Lens Flips Across Recorded Sessions
Arrays & HashingAuto-graded · 15 test casesLast updated
Group event logs by session, sort chronologically, count adjacent front-then-rear transitions, and average over all sessions, including sessions with zero flips.
A camera app records every time a user selects a lens during a usage session. Each lens selection is either "front" (selfie) or "rear" (world-facing).
You are given a list of event records. Each record is a list of four values:
[event_id, session_id, camera_mode, event_time]
event_idis an integer identifying the event.session_idis a string identifying the session the event belongs to.camera_modeis either"front"or"rear".event_timeis a timestamp string in the format"YYYY-MM-DD HH:MM:SS".
The records may arrive in any order.
Task
Compute the average number of front-to-rear flips per session.
Within a single session, sort the events by event_time in ascending order. A front-to-rear flip occurs at an event whose mode is "rear" when the event directly before it in that same session's sorted order has mode "front". Events from different sessions never form a flip together. Only the adjacent pair counts: front, front, rear yields one flip, and rear, front yields none.
If two events in the same session have the same event_time, keep them in the order they appeared in the input.
Every distinct session_id that appears in the input counts toward the average, including sessions with a single event and sessions with no flips.
Output
Return the total number of flips across all sessions divided by the number of distinct sessions, as a float rounded to 2 decimal places. If the input list is empty, return 0.0.
Constraints
0 <= len(rows)camera_modeis always"front"or"rear".- Timestamps use a fixed-width format, so comparing them as strings gives chronological order.
- Use true (floating-point) division, not integer division.
- Round the final result to 2 decimal places.
Examples
Example 1
Input: mean_front_to_rear_flips([[1, 's1', 'front', '2024-01-01 10:00:00'], [2, 's1', 'rear', '2024-01-01 10:00:05'], [3, 's1', 'front', '2024-01-01 10:00:10'], [4, 's1', 'rear', '2024-01-01 10:00:15'], [5, 's2', 'rear', '2024-01-01 11:00:00'], [6, 's2', 'front', '2024-01-01 11:00:04'], [7, 's3', 'front', '2024-01-01 12:00:00']])
Output: 0.67
Hints
Hint 1
Group the records by session_id first; flips never cross a session boundary.
Hint 2
Within each group, sort by event_time (a stable sort keeps input order for ties), then scan adjacent pairs looking for front followed by rear.
Hint 3
The average over sessions equals total flips divided by the number of distinct sessions, so sessions with zero flips must still be counted in the denominator. Handle the empty input separately.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
- If there are no rows, return
0.0. - Group events by
session_id, storing(event_time, camera_mode)for each. - Sort each group by
event_time. Python's sort is stable, so events with equal timestamps keep their input order. - Walk through each sorted group and count positions where the previous mode is
"front"and the current mode is"rear". - Divide the total flip count by the number of distinct sessions using true division, and round to 2 decimals.
Why it is correct
- Grouping first guarantees that a flip is only ever detected between two events of the same session.
- Checking only adjacent pairs in sorted order matches the definition exactly: for
front, front, rear, only the finalrearhasfrontright before it, so one flip is counted. Arear, frontpair is never counted. - The mean of per-session counts equals total flips divided by the number of sessions, so one running total is enough.
- The denominator is the size of the session dictionary, which includes every session seen, even those with a single event or zero flips. These sessions are not dropped.
Complexity
Let n be the number of events. Sorting within groups costs O(n log n) overall in the worst case, and the scan is O(n). Space is O(n) for the grouped events.
Reference solution
from typing import List
def mean_front_to_rear_flips(rows: List[List]) -> float:
if not rows:
return 0.0
sessions = {}
for event_id, session_id, camera_mode, event_time in rows:
sessions.setdefault(session_id, []).append((event_time, camera_mode))
total_flips = 0
for events in sessions.values():
# Stable sort: ties on event_time keep input order
events.sort(key=lambda e: e[0])
for i in range(1, len(events)):
if events[i - 1][1] == "front" and events[i][1] == "rear":
total_flips += 1
return round(total_flips / len(sessions), 2)