Return Top Search Queries per Country Including Ties in Pandas
Last updated: November 29, 2026
Quick Overview
Given a log of (country, query) rows, return the top k queries per country by frequency after normalizing case and whitespace, keeping all queries tied at the cutoff.
0
0
0 solved
Given a log of (country, query) rows, return the top k queries per country by frequency after normalizing case and whitespace, keeping all queries tied at the cutoff.
This is a warm-up in the style of Google data science phone screens on search logs. The interviewer checks normalization, grouped counting, rank methods for ties, and stable ordering of the output.
What the Interviewer Expects
- Normalize queries before counting
- Group by country and query to count
- Use rank with method min to keep ties
- Sort output deterministically
- Mention scale considerations
Key Topics to Cover
How to Approach This
- Clarify the schema and expected output format before writing queries.
- Use CTEs (WITH clauses) to break complex queries into readable steps.
- Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
- Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
- For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
- How would you do this in SQL?
- What if you must return exactly k rows per country?
- How would you compute this over a stream of queries?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice SQL ProblemsSample Answer
Clarify the Data
logs DataFrame with columns country, query. Normalization: lowercase and strip surrounding whitespace; drop empty queries. "Top k with ties" means include every query whose count is at least the...
Approach
Normalize, count per (country, query), rank counts within each country descending with method="min" (ties share the best rank, like SQL RANK), filter rank at most k, and sort by country, count des...