Calculate QR Campaign Funnel Metrics
Math & Bit ManipulationAuto-graded · 21 test casesLast updated
Tests understanding of multi-stage conversion funnels by computing metrics at each step given input rates.
You are analyzing a marketing campaign that displays a QR code during a televised event. Your task is to calculate key funnel metrics at each stage of user engagement.
Given the total number of viewers and rates for each conversion step, compute the number of people at each stage:
- Device-equipped viewers: viewers who own a smartphone
- Attentive viewers: device-equipped viewers who are actually watching the screen during the ad
- Scans: attentive viewers who scan the QR code
- Uses: scanners who complete the desired action (e.g., sign up, redeem offer)
Write a function that takes five parameters:
total_viewers(int): the total audience sizedevice_rate(float): the fraction of viewers who own a smartphone (0.0 to 1.0)attention_rate(float): the fraction of device owners who are watching during the ad (0.0 to 1.0)scan_rate(float): the fraction of attentive device owners who scan the code (0.0 to 1.0)conversion_rate(float): the fraction of scanners who complete the final action (0.0 to 1.0)
Return a dictionary with four keys:
"device_viewers": number after applying device_rate"attentive_viewers": number after applying attention_rate to device_viewers"scans": number after applying scan_rate to attentive_viewers"uses": number after applying conversion_rate to scans
All output values should be floats representing the computed counts at each stage.
Constraints
1 ≤ total_viewers ≤ 10^90.0 ≤ device_rate ≤ 1.00.0 ≤ attention_rate ≤ 1.00.0 ≤ scan_rate ≤ 1.00.0 ≤ conversion_rate ≤ 1.0- Each rate represents a valid probability
- Output values should preserve floating-point precision (no rounding required)
Examples
Example 1
Input: calculate_qr_funnel(100000000, 0.85, 0.2, 0.15, 0.1)
Output: {'device_viewers': 85000000, 'attentive_viewers': 17000000, 'scans': 2550000, 'uses': 255000}
Example 2
Input: calculate_qr_funnel(50000000, 0.9, 0.3, 0.25, 0.08)
Output: {'device_viewers': 45000000, 'attentive_viewers': 13500000, 'scans': 3375000, 'uses': 270000}
Example 3
Input: calculate_qr_funnel(120000000, 0.75, 0.1, 0.05, 0.2)
Output: {'device_viewers': 90000000, 'attentive_viewers': 9000000, 'scans': 450000, 'uses': 90000}
Hints
Hint 1
Each stage applies its rate to the output of the previous stage, forming a sequential funnel
Hint 2
The calculation flows: total → device owners → attentive → scans → uses
Hint 3
Multiply the cumulative audience by each rate in sequence; track all four intermediate results
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem models a standard conversion funnel where each stage filters the audience from the previous stage. We need to apply each rate sequentially and track the population at every step.
The calculation proceeds in four stages:
- Start with the total viewer count
- Apply the device ownership rate to get device-equipped viewers
- Apply the attention rate to device viewers to get those actually watching
- Apply the scan rate to attentive viewers to get scan count
- Apply the conversion rate to scans to get final action completions
Each multiplication reduces the population by the given fraction. Since rates are between 0 and 1, each subsequent stage will have fewer or equal people compared to the previous stage.
The key insight is that this is a multiplicative funnel: each stage's output becomes the next stage's input. We compute all four intermediate values and return them in the required dictionary format.
Why this works: Each rate represents a conditional probability (e.g., "what fraction of device owners are attentive?"), so multiplying by the previous stage gives us the absolute count at each step.
Time Complexity: O(1) — we perform exactly four multiplications and construct one dictionary.
Space Complexity: O(1) — we store four intermediate values and return a fixed-size dictionary.
from typing import Dict
def calculate_qr_funnel(total_viewers: int, device_rate: float, attention_rate: float, scan_rate: float, conversion_rate: float) -> Dict[str, float]:
device_viewers = total_viewers * device_rate
attentive_viewers = device_viewers * attention_rate
scans = attentive_viewers * scan_rate
uses = scans * conversion_rate
return {
"device_viewers": device_viewers,
"attentive_viewers": attentive_viewers,
"scans": scans,
"uses": uses
}