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.

Google
Data Manipulation (SQL/Python)
Data Scientist
Google
November 29, 2026
Data Scientist
Phone Screen
Data Manipulation (SQL/Python)
Easy

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
Pandas groupby
Ranking with ties
Text normalization
Top-k
Deterministic ordering
How to Approach This
  1. Clarify the schema and expected output format before writing queries.
  2. Use CTEs (WITH clauses) to break complex queries into readable steps.
  3. Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
  4. Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
  5. 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 Problems
Sample 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...


Submit Your Answer
Markdown supported