Skip to content
tezvyn:

How do duplicate events bias COUNT(*) and daily login reports?

Source: cloud.google.comMediumHow cards are made

How do duplicate events bias COUNT(*) and daily login reports?

Tests idempotency in streaming analytics. COUNT(*) overcounts; fix with unique event ID dedup via idempotent writes or COUNT(DISTINCT id), plus daily partition reconciliation. Red flag: SELECT DISTINCT * without a stable key or no reporting safeguard.

What's really being asked

This question tests whether you understand that at-least-once delivery is the default in most distributed streaming systems and that analytics must be defensive rather than naive. Interviewers want to see if you distinguish between pipeline correctness and query correctness, and if you can design an idempotent reporting layer that survives duplicates without requiring exactly-once guarantees from the transport.

The full answer

First, the impact. A simple COUNT() on login events will overstate daily active users because every duplicate row is counted as an additional login. If the duplicate rate is five percent, your report is off by five percent, which compounds in downstream dashboards. Second, the strategy. The best answers propose a unique event identifier generated at the source, such as a UUID or a composite key of user_id, timestamp, and a client nonce. Then they describe two complementary layers. At the storage layer, use idempotent writes like INSERT OVERWRITE or MERGE with the event ID as the join key so reruns do not create new rows. At the query layer, use COUNT(DISTINCT event_id) instead of COUNT() so duplicates collapse to one. Third, daily partition reconciliation. Because duplicates may arrive late, the strategy should include reprocessing the last N partitions or using a watermark so daily reports can be backfilled deterministically.

The mistakes people make

Saying the impact is negligible without quantifying it is a red flag. Proposing SELECT DISTINCT * is wrong because two duplicate rows may differ slightly in ingestion metadata like arrival time, so DISTINCT on all columns will not collapse them. Suggesting to fix the pipeline and drop duplicates upstream is incomplete because the question asks how to ensure reports are accurate despite duplicates, which implies the pipeline may not be fully fixed. Ignoring late-arriving duplicates and assuming a once-a-day batch window catches everything is another miss.

What usually comes next

The interviewer might ask how you detect the duplicate rate in practice, how you handle events without a natural unique key, or what tradeoffs exist between COUNT(DISTINCT) and approximate cardinality estimators like HyperLogLog. They may also ask how to reconcile late arrivals that land in yesterday's partition without rewriting the entire table, or how this changes if you move from daily batch to real-time materialized views.

A concrete example

Imagine a Pub/Sub to BigQuery pipeline where subscriber retries create two percent duplicate logins. Instead of COUNT(*), you define the BigQuery table with a merge key on event_id and use a MERGE statement in your daily Airflow job: insert new events, ignore matches. Your report query then selects COUNT(DISTINCT event_id) from the partitioned table where date equals current_date. If late events arrive, you rerun the merge for the last three partitions, and the report updates idempotently because the merge key prevents double counting.

Interview question

Which approach correctly prevents duplicate login events from inflating daily active user counts in a pipeline with at-least-once delivery?

  • a.Process daily batches once per day and assume late-arriving duplicates never cross partition boundaries
  • b.Switch the streaming platform to exactly-once delivery and use COUNT(*) on the raw stream
  • c.Run SELECT DISTINCT * on the raw table before counting to remove duplicate rows
  • d.Merge events using a unique event ID as the key and query with COUNT(DISTINCT event_id)Correct
Why?

The correct answer builds an idempotent reporting layer: merging on a stable event ID prevents storage-level duplicates, while COUNT(DISTINCT event_id) collapses any remaining duplicates at query time. Option C is wrong because duplicate rows often differ in ingestion metadata like arrival time, so SELECT DISTINCT * will not treat them as duplicates.

Just read this? Test yourself on what you have been reading.

Read the original → cloud.google.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on analytics — each one lists the topics its interview covers.

See open roles