Sum Investment Values after Two Independent Group Tests
Company: Meta
Role: Software Engineer
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
### Problem
Report tiv_2016, rounded to two decimal places, summing policies that share their tiv_2015 with at least one other policy and whose exact (lat,lon) location occurs for no other policy. Both tests refer to the entire Insurance table. The exact sum fits the PostgreSQL numeric range.
### Tables
Table: `Insurance`
```text
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| pid | int |
| tiv_2015 | numeric |
| tiv_2016 | numeric |
| lat | float |
| lon | float |
+-------------+---------+
pid is the primary key (column with unique values) for this table.
Each row of this table contains information about one policy where:
pid is the policyholder's policy ID.
tiv_2015 is the total investment value in 2015 and tiv_2016 is the total investment value in 2016.
lat is the latitude of the policy holder's city. It's guaranteed that lat is not NULL.
lon is the longitude of the policy holder's city. It's guaranteed that lon is not NULL.
```
### Constraints
- In this version, `tiv_2015` and `tiv_2016` are exact decimal values represented by PostgreSQL `numeric`; both are finite and non-NULL.
- In this version, `lat` and `lon` remain floating-point coordinates and are finite and non-NULL.
- Investment values and all intermediate and final aggregate sums, including the rounded result, fit PostgreSQL `numeric`.
### Output
Return exactly one row with the column `tiv_2016`. If no policies qualify, that value is NULL; otherwise, return the sum rounded to two decimal places.
### Example
```text
Insurance (pid,tiv_2015,tiv_2016,lat,lon):
(1,10,12,1,1), (2,10,18,2,2), (3,20,30,2,2), (4,30,40,3,3)
Output (tiv_2016): (12.00)
```
### What a Strong Answer Covers
- Evaluate both eligibility rules against the complete policy table.
- Sum the qualifying investments and return the specified NULL result when none qualify.
### Follow-up Questions
- How would you report the qualifying policy count alongside the sum?
- How would approximate location matching change the definition of a unique location?
Overview: Sum qualifying investments when an earlier investment value is shared and the policy location is unique.
Report tiv_2016, rounded to two decimal places, summing policies that share their tiv_2015 with at least one other policy and whose exact (lat,lon) location occurs for no other policy. Both tests refer to the entire Insurance table. The exact sum fits the PostgreSQL numeric range.
Tables
Table: Insurance
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| pid | int |
| tiv_2015 | numeric |
| tiv_2016 | numeric |
| lat | float |
| lon | float |
+-------------+---------+
pid is the primary key (column with unique values) for this table.
Each row of this table contains information about one policy where:
pid is the policyholder's policy ID.
tiv_2015 is the total investment value in 2015 and tiv_2016 is the total investment value in 2016.
lat is the latitude of the policy holder's city. It's guaranteed that lat is not NULL.
lon is the longitude of the policy holder's city. It's guaranteed that lon is not NULL.
Constraints
In this version,
tiv_2015
and
tiv_2016
are exact decimal values represented by PostgreSQL
numeric
; both are finite and non-NULL.
In this version,
lat
and
lon
remain floating-point coordinates and are finite and non-NULL.
Investment values and all intermediate and final aggregate sums, including the rounded result, fit PostgreSQL
numeric
.
Output
Return exactly one row with the column tiv_2016. If no policies qualify, that value is NULL; otherwise, return the sum rounded to two decimal places.