Reviewed by Aditya Kumar · Last reviewed 2026-08-08
To determine the number of cities each department operates in and list the top 3, you'll group the data by department, count the distinct cities, and then order the results to select the top entries,…
This medium-level Python/Coding question appears frequently in data engineering interviews at companies like Freight Tiger. While less common, it tests deeper understanding that distinguishes strong candidates. Mastering the underlying concepts (sql) will help you answer variations of this question confidently.
Break this problem into components. Identify the core trade-offs involved, then walk the interviewer through your reasoning step by step. Demonstrate awareness of edge cases and production considerations - this is what separates good answers from great ones. The expert answer includes a code example that demonstrates the implementation pattern.
To determine the number of cities each department operates in and list the top 3, you'll group the data by department, count the distinct cities, and then order the results to select the top entries, handling ties as specified.
GROUP BY dept_id combines all records for a given department. Within each group, COUNT(DISTINCT city) calculates the unique number of cities. To identify the "top 3," the results are then sorted by city_count in descending order. The question explicitly states, "In case of a tie, order by dept_id," which means dept_id should be used as a secondary sort key in ascending order. Finally, LIMIT 3 selects only the top three departments based on this sorting logic.
While window functions like RANK() or DENSE_RANK() could be used to assign ranks, the ORDER BY ... LIMIT 3 approach directly answers the question's requirement for exactly three departments, using dept_id to deterministically break ties if multiple departments share the same city_count at the third position.
department_operations with columns dept_id and city.
SELECT
dept_id,
COUNT(DISTINCT city) AS city_count
FROM
department_operations
GROUP BY
dept_id
ORDER BY
city_count DESC,
dept_id ASC
LIMIT 3;
Performance Considerations:COUNT(DISTINCT) can be an expensive operation, especially on large datasets, as it often requires a full data scan and significant data shuffling across nodes to ensure uniqueness before aggregation. In systems like Apache Spark, this translates to high network I/O and CPU usage during the shuffle phase. For data warehouses like Snowflake, while micro-partition pruning can optimize initial data access, the aggregation still demands computational resources. GROUP BY and ORDER BY operations also involve sorting and potential shuffling, impacting query performance. For frequently queried top-N metrics, consider pre-aggregating this data into a materialized view or a dbt model.
LIMIT 3) or "up to three ranks" (which might return more than three rows if multiple departments tie for the 3rd rank, requiring RANK() or DENSE_RANK() and filtering). Also, discuss the implications of data volume on query performance and potential optimization strategies for very large datasets.Pro-Move: RANK vs DENSE_RANK. Red Flag: LIMIT 3 without tie handling.
Some links below are affiliate links. If you buy through them we may earn a small commission at no extra cost to you — it helps keep DataEngPrep free.
According to DataEngPrep.tech, this is one of the most frequently asked Python/Coding interview questions, reported at 1 company. DataEngPrep.tech maintains an editor-reviewed database of 1,863 data engineering interview questions across 7 categories.