LeetCode #3580 — MEDIUM

Find Consistently Improving Employees

Move from brute-force thinking to an efficient approach using core interview patterns strategy.

Solve on LeetCode
The Problem

Problem Statement

Table: employees

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| employee_id | int     |
| name        | varchar |
+-------------+---------+
employee_id is the unique identifier for this table.
Each row contains information about an employee.

Table: performance_reviews

+-------------+------+
| Column Name | Type |
+-------------+------+
| review_id   | int  |
| employee_id | int  |
| review_date | date |
| rating      | int  |
+-------------+------+
review_id is the unique identifier for this table.
Each row represents a performance review for an employee. The rating is on a scale of 1-5 where 5 is excellent and 1 is poor.

Write a solution to find employees who have consistently improved their performance over their last three reviews.

  • An employee must have at least 3 review to be considered
  • The employee's last 3 reviews must show strictly increasing ratings (each review better than the previous)
  • Use the most recent 3 reviews based on review_date for each employee
  • Calculate the improvement score as the difference between the latest rating and the earliest rating among the last 3 reviews

Return the result table ordered by improvement score in descending order, then by name in ascending order.

The result format is in the following example.

Example:

Input:

employees table:

+-------------+----------------+
| employee_id | name           |
+-------------+----------------+
| 1           | Alice Johnson  |
| 2           | Bob Smith      |
| 3           | Carol Davis    |
| 4           | David Wilson   |
| 5           | Emma Brown     |
+-------------+----------------+

performance_reviews table:

+-----------+-------------+-------------+--------+
| review_id | employee_id | review_date | rating |
+-----------+-------------+-------------+--------+
| 1         | 1           | 2023-01-15  | 2      |
| 2         | 1           | 2023-04-15  | 3      |
| 3         | 1           | 2023-07-15  | 4      |
| 4         | 1           | 2023-10-15  | 5      |
| 5         | 2           | 2023-02-01  | 3      |
| 6         | 2           | 2023-05-01  | 2      |
| 7         | 2           | 2023-08-01  | 4      |
| 8         | 2           | 2023-11-01  | 5      |
| 9         | 3           | 2023-03-10  | 1      |
| 10        | 3           | 2023-06-10  | 2      |
| 11        | 3           | 2023-09-10  | 3      |
| 12        | 3           | 2023-12-10  | 4      |
| 13        | 4           | 2023-01-20  | 4      |
| 14        | 4           | 2023-04-20  | 4      |
| 15        | 4           | 2023-07-20  | 4      |
| 16        | 5           | 2023-02-15  | 3      |
| 17        | 5           | 2023-05-15  | 2      |
+-----------+-------------+-------------+--------+

Output:

+-------------+----------------+-------------------+
| employee_id | name           | improvement_score |
+-------------+----------------+-------------------+
| 2           | Bob Smith      | 3                 |
| 1           | Alice Johnson  | 2                 |
| 3           | Carol Davis    | 2                 |
+-------------+----------------+-------------------+

Explanation:

  • Alice Johnson (employee_id = 1):
    • Has 4 reviews with ratings: 2, 3, 4, 5
    • Last 3 reviews (by date): 2023-04-15 (3), 2023-07-15 (4), 2023-10-15 (5)
    • Ratings are strictly increasing: 3 → 4 → 5
    • Improvement score: 5 - 3 = 2
  • Carol Davis (employee_id = 3):
    • Has 4 reviews with ratings: 1, 2, 3, 4
    • Last 3 reviews (by date): 2023-06-10 (2), 2023-09-10 (3), 2023-12-10 (4)
    • Ratings are strictly increasing: 2 → 3 → 4
    • Improvement score: 4 - 2 = 2
  • Bob Smith (employee_id = 2):
    • Has 4 reviews with ratings: 3, 2, 4, 5
    • Last 3 reviews (by date): 2023-05-01 (2), 2023-08-01 (4), 2023-11-01 (5)
    • Ratings are strictly increasing: 2 → 4 → 5
    • Improvement score: 5 - 2 = 3
  • Employees not included:
    • David Wilson (employee_id = 4): Last 3 reviews are all 4 (no improvement)
    • Emma Brown (employee_id = 5): Only has 2 reviews (needs at least 3)

The output table is ordered by improvement_score in descending order, then by name in ascending order.

Roadmap

  1. Brute Force Baseline
  2. Core Insight
  3. Algorithm Walkthrough
  4. Edge Cases
  5. Full Annotated Code
  6. Interactive Study Demo
  7. Complexity Analysis
Step 01

Brute Force Baseline

Problem summary: Table: employees +-------------+---------+ | Column Name | Type | +-------------+---------+ | employee_id | int | | name | varchar | +-------------+---------+ employee_id is the unique identifier for this table. Each row contains information about an employee. Table: performance_reviews +-------------+------+ | Column Name | Type | +-------------+------+ | review_id | int | | employee_id | int | | review_date | date | | rating | int | +-------------+------+ review_id is the unique identifier for this table. Each row represents a performance review for an employee. The rating is on a scale of 1-5 where 5 is excellent and 1 is poor. Write a solution to find employees who have consistently improved their performance over their last three reviews. An employee must have at least 3 review to be considered The employee's last 3 reviews must show strictly increasing ratings (each review better

Baseline thinking

Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.

Pattern signal: General problem-solving

Example 1

{"headers":{"employees":["employee_id","name"],"performance_reviews":["review_id","employee_id","review_date","rating"]},"rows":{"employees":[[1,"Alice Johnson"],[2,"Bob Smith"],[3,"Carol Davis"],[4,"David Wilson"],[5,"Emma Brown"]],"performance_reviews":[[1,1,"2023-01-15",2],[2,1,"2023-04-15",3],[3,1,"2023-07-15",4],[4,1,"2023-10-15",5],[5,2,"2023-02-01",3],[6,2,"2023-05-01",2],[7,2,"2023-08-01",4],[8,2,"2023-11-01",5],[9,3,"2023-03-10",1],[10,3,"2023-06-10",2],[11,3,"2023-09-10",3],[12,3,"2023-12-10",4],[13,4,"2023-01-20",4],[14,4,"2023-04-20",4],[15,4,"2023-07-20",4],[16,5,"2023-02-15",3],[17,5,"2023-05-15",2]]}}
Step 02

Core Insight

What unlocks the optimal approach

  • No official hints in dataset. Start from constraints and look for a monotonic or reusable state.
Interview move: turn each hint into an invariant you can check after every iteration/recursion step.
Step 03

Algorithm Walkthrough

Iteration Checklist

  1. Define state (indices, window, stack, map, DP cell, or recursion frame).
  2. Apply one transition step and update the invariant.
  3. Record answer candidate when condition is met.
  4. Continue until all input is consumed.
Use the first example testcase as your mental trace to verify each transition.
Step 04

Edge Cases

Minimum Input
Single element / shortest valid input
Validate boundary behavior before entering the main loop or recursion.
Duplicates & Repeats
Repeated values / repeated states
Decide whether duplicates should be merged, skipped, or counted explicitly.
Extreme Constraints
Upper-end input sizes
Re-check complexity target against constraints to avoid time-limit issues.
Invalid / Corner Shape
Empty collections, zeros, or disconnected structures
Handle special-case structure before the core algorithm path.
Step 05

Full Annotated Code

Source-backed implementations are provided below for direct study and interview prep.

// Accepted solution for LeetCode #3580: Find Consistently Improving Employees
// Auto-generated Java example from py.
class Solution {
    public void exampleSolution() {
    }
}
// Reference (py):
// # Accepted solution for LeetCode #3580: Find Consistently Improving Employees
// import pandas as pd
// 
// 
// def find_consistently_improving_employees(
//     employees: pd.DataFrame, performance_reviews: pd.DataFrame
// ) -> pd.DataFrame:
//     performance_reviews = performance_reviews.sort_values(
//         ["employee_id", "review_date"], ascending=[True, False]
//     )
//     performance_reviews["rn"] = (
//         performance_reviews.groupby("employee_id").cumcount() + 1
//     )
//     performance_reviews["lag_rating"] = performance_reviews.groupby("employee_id")[
//         "rating"
//     ].shift(1)
//     performance_reviews["delta"] = (
//         performance_reviews["lag_rating"] - performance_reviews["rating"]
//     )
//     recent = performance_reviews[
//         (performance_reviews["rn"] > 1) & (performance_reviews["rn"] <= 3)
//     ]
//     improvement = (
//         recent.groupby("employee_id")
//         .agg(
//             improvement_score=("delta", "sum"),
//             count=("delta", "count"),
//             min_delta=("delta", "min"),
//         )
//         .reset_index()
//     )
//     improvement = improvement[
//         (improvement["count"] == 2) & (improvement["min_delta"] > 0)
//     ]
//     result = improvement.merge(employees[["employee_id", "name"]], on="employee_id")
//     result = result.sort_values(
//         by=["improvement_score", "name"], ascending=[False, True]
//     )
//     return result[["employee_id", "name", "improvement_score"]]
Step 06

Interactive Study Demo

Use this to step through a reusable interview workflow for this problem.

Press Step or Run All to begin.
Step 07

Complexity Analysis

Time
O(n)
Space
O(1)

Approach Breakdown

BRUTE FORCE
O(n²) time
O(1) space

Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.

OPTIMIZED
O(n) time
O(1) space

Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.

Shortcut: If you are using nested loops on an array, there is almost always an O(n) solution. Look for the right auxiliary state.
Coach Notes

Common Mistakes

Review these before coding to avoid predictable interview regressions.

Off-by-one on range boundaries

Wrong move: Loop endpoints miss first/last candidate.

Usually fails on: Fails on minimal arrays and exact-boundary answers.

Fix: Re-derive loops from inclusive/exclusive ranges before coding.