Given a SurveyLog table that tracks user actions on survey questions, find the question with the highest answer rate.
The table contains:
id: User IDaction: One of 'show', 'answer', or 'skip'question_id: ID of the questionanswer_id: ID of the answer (only for 'answer' actions)q_num: Question order in the sessiontimestamp: When the action occurred
The answer rate for a question is calculated as:
Answer Rate = (Number of 'answer' actions) / (Number of 'show' actions)
Return the question_id with the highest answer rate. If there's a tie, return the question with the smallest question_id.
Table Schema
| Column Name | Type | Description |
|---|---|---|
id
|
int | User ID who performed the action |
action
|
varchar | Action type: 'show', 'answer', or 'skip' |
question_id
|
int | ID of the question being acted upon |
answer_id
|
int | ID of the answer (NULL for non-answer actions) |
q_num
|
int | Sequential order of question in the session |
timestamp
|
int | Unix timestamp when action occurred |
Input & Output
| id | action | question_id | answer_id | q_num | timestamp |
|---|---|---|---|---|---|
| 5 | show | 285 | 1 | 123 | |
| 5 | answer | 285 | 124 | 1 | 124 |
| 5 | show | 369 | 2 | 125 | |
| 5 | skip | 369 | 2 | 126 |
| question_id |
|---|
| 285 |
Question 285: 1 answer out of 1 show = 100% answer rate
Question 369: 0 answers out of 1 show = 0% answer rate
Question 285 has the highest answer rate, so it's returned.
| id | action | question_id | answer_id | q_num | timestamp |
|---|---|---|---|---|---|
| 1 | show | 100 | 1 | 100 | |
| 1 | answer | 100 | 50 | 1 | 101 |
| 2 | show | 200 | 1 | 102 | |
| 2 | answer | 200 | 75 | 1 | 103 |
| question_id |
|---|
| 100 |
Both questions have 100% answer rate (1 answer / 1 show each)
Since there's a tie, we return the question with the smallest question_id
Question 100 < Question 200, so question 100 is returned.
| id | action | question_id | answer_id | q_num | timestamp |
|---|---|---|---|---|---|
| 1 | show | 555 | 1 | 200 | |
| 1 | show | 555 | 1 | 201 | |
| 1 | answer | 555 | 10 | 1 | 202 |
| 2 | show | 777 | 1 | 203 | |
| 2 | skip | 777 | 1 | 204 |
| question_id |
|---|
| 555 |
Question 555: 1 answer out of 2 shows = 50% answer rate
Question 777: 0 answers out of 1 show = 0% answer rate
Question 555 has the higher answer rate and is returned.
Constraints
-
1 ≤ id ≤ 1000 -
actionis one of'show','answer', or'skip' -
1 ≤ question_id ≤ 1000 -
answer_idis NULL for non-answer actions - At least one question will have both 'show' and 'answer' actions