A cab booking company has a dataset of its trip ratings (`rating_table`). Each row represents a single trip of a driver. A trip has a positive rating if it was rated 4 or above. A streak of positive ratings is when a driver has a rating of 4 or above in consecutive trips. For example, if there are 3 consecutive trips with a rating of 4 or above, then the streak is 3.

Table name - `rating_table`

[tex]\[
\begin{tabular}{|l|l|l|}
\hline
\textbf{Column Name} & \textbf{Description} & \textbf{Datatype} \\
\hline
trip_time & Timestamp of trip & timestamp \\
\hline
driver_id & ID of driver & varchar \\
\hline
trip_id & ID of trip & varchar \\
\hline
rating & Rating of the trip & integer \\
\hline
\end{tabular}
\][/tex]

Sample Data:

[tex]\[
\begin{tabular}{|l|l|l|l|}
\hline
trip_time & driver_id & trip_id & rating \\
\hline
2023-04-24 15:20:27 & a & 1 & 4 \\
\hline
2023-04-24 22:32:27 & a & 2 & 5 \\
\hline
2023-04-25 08:08:27 & a & 3 & 3 \\
\hline
2023-04-25 12:56:27 & a & 4 & 5 \\
\hline
2023-04-25 17:44:27 & a & 5 & 4 \\
\hline
2023-04-25 22:32:27 & a & 6 & 5 \\
\hline
\end{tabular}
\][/tex]

Question:

Find out the maximum streak that a driver has had. Rank the drivers in descending order of their maximum streak and then by descending order of driver ID. Note: Only users who have at least one streak should be included in the output.

Output Format:

[tex]\[
\begin{tabular}{|l|l|}
\hline
driver_id & max_streak \\
\hline
& \\
\hline
& \\
\hline
\end{tabular}
\][/tex]



Answer :

Sure, let's analyze the given problem step by step and derive the solution.

### Problem Understanding
We need to determine the maximum streak of positive ratings (ratings 4 or above) for each driver and rank these drivers in descending order of their maximum streak. If two drivers have the same streak length, they should be sorted in descending order of their driver IDs.

### Steps to Solve the Problem:

1. Create a Data Structure for Streaks:
- We'll use a data structure to keep track of streaks for each driver. A dictionary can hold the driver IDs as keys and lists of streaks as values.

2. Iterate through the Rating Data:
- For each rating, we update the streak record. If a rating is 4 or above, we increment the current streak. If it is below 4, we end the current streak and start a new one next time a rating of 4 or higher appears.

3. Calculate Maximum Streaks:
- Once we have the streaks for each driver, we find the maximum streak for each driver.

4. Sort and Format the Output:
- Finally, we sort the drivers first by the maximum streak in descending order and then by driver ID in descending order (if streaks are equal). Then, we format the output as required.

### Solution

Given the dataset:
```
[
("2023-04-24 15:20:27", "a", 2),
("2023-04-24 22:32:27", "a", 3),
("2023-04-25 08:08:27", "a", 3),
("2023-04-25 12:56:27", "a", 4),
("2023-04-25 17:44:27", "a", 5),
("2023-04-25 22:32:27", "a", 6)
]
```

### Step-by-Step Solution

1. Track Streaks:
- Initialize an empty dictionary called `driver_streaks`.
- Iterate through the `rating_table`. For each row:
- If the rating is 4 or higher, increase the streak for the respective driver.
- If the rating is below 4, reset the streak counter.

Let's manually check the ratings for driver "a":
- First three ratings are below 4, so no streak starts.
- For "2023-04-25 12:56:27", "a" has a rating of 4 — Start of a streak.
- Next rating is 5 — streak continues.
- Next rating is 6 — streak continues.
- Streak count = 3 (4 followed by 5 followed by 6).

2. Calculate Maximum Streak:
- For driver "a", the maximum streak is 3.

3. Sort and Output Format:
- Since there's only one driver, the sorted order remains as is.
- The final output is in the form of a list of tuples: `[("a", 3)]`.

### Conclusion

Thus, the maximum streak of consecutive positive ratings for driver "a" is 3. In ranked descending order, the output is:

```
driver_id max_streak
a 3
```

This result follows from tracing through the data and tallying the streaks as described.