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.
Build confidence with an intuition-first walkthrough focused on core interview patterns fundamentals.
Table: Person
+-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | email | varchar | +-------------+---------+ id is the primary key (column with unique values) for this table. Each row of this table contains an email. The emails will not contain uppercase letters.
Write a solution to delete all duplicate emails, keeping only one unique email with the smallest id.
For SQL users, please note that you are supposed to write a DELETE statement and not a SELECT one.
For Pandas users, please note that you are supposed to modify Person in place.
After running your script, the answer shown is the Person table. The driver will first compile and run your piece of code and then show the Person table. The final order of the Person table does not matter.
The result format is in the following example.
Example 1:
Input: Person table: +----+------------------+ | id | email | +----+------------------+ | 1 | john@example.com | | 2 | bob@example.com | | 3 | john@example.com | +----+------------------+ Output: +----+------------------+ | id | email | +----+------------------+ | 1 | john@example.com | | 2 | bob@example.com | +----+------------------+ Explanation: john@example.com is repeated two times. We keep the row with the smallest Id = 1.
Problem summary: Table: Person +-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | email | varchar | +-------------+---------+ id is the primary key (column with unique values) for this table. Each row of this table contains an email. The emails will not contain uppercase letters. Write a solution to delete all duplicate emails, keeping only one unique email with the smallest id. For SQL users, please note that you are supposed to write a DELETE statement and not a SELECT one. For Pandas users, please note that you are supposed to modify Person in place. After running your script, the answer shown is the Person table. The driver will first compile and run your piece of code and then show the Person table. The final order of the Person table does not matter. The result format is in the following example.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
{"headers": {"Person": ["id", "email"]}, "rows": {"Person": [[1, "john@example.com"], [2, "bob@example.com"], [3, "john@example.com"]]}}Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #196: Delete Duplicate Emails
// Auto-generated Java example from py.
class Solution {
public void exampleSolution() {
}
}
// Reference (py):
// # Accepted solution for LeetCode #196: Delete Duplicate Emails
// import pandas as pd
//
//
// # Modify Person in place
// def delete_duplicate_emails(person: pd.DataFrame) -> None:
// # Sort the rows based on id (Ascending order)
// person.sort_values(by="id", ascending=True, inplace=True)
// # Drop the duplicates based on email.
// person.drop_duplicates(subset="email", keep="first", inplace=True)
// Accepted solution for LeetCode #196: Delete Duplicate Emails
// Auto-generated Go example from py.
func exampleSolution() {
}
// Reference (py):
// # Accepted solution for LeetCode #196: Delete Duplicate Emails
// import pandas as pd
//
//
// # Modify Person in place
// def delete_duplicate_emails(person: pd.DataFrame) -> None:
// # Sort the rows based on id (Ascending order)
// person.sort_values(by="id", ascending=True, inplace=True)
// # Drop the duplicates based on email.
// person.drop_duplicates(subset="email", keep="first", inplace=True)
# Accepted solution for LeetCode #196: Delete Duplicate Emails
import pandas as pd
# Modify Person in place
def delete_duplicate_emails(person: pd.DataFrame) -> None:
# Sort the rows based on id (Ascending order)
person.sort_values(by="id", ascending=True, inplace=True)
# Drop the duplicates based on email.
person.drop_duplicates(subset="email", keep="first", inplace=True)
// Accepted solution for LeetCode #196: Delete Duplicate Emails
struct Solution;
impl Solution {
pub fn next_greater_element(nums1: Vec<i32>, nums2: Vec<i32>) -> Vec<i32> {
pub fn next_greater(num: i32, nums2: &[i32]) -> i32 {
let mut flag = false;
for i in nums2 {
if i == &num {
flag = true;
}
if flag == true && i > &num {
return *i;
}
}
return -1;
}
nums1.iter().map(|num|next_greater(*num, &nums2)).collect::<Vec<i32>>()
}
}
fn main() {
let nums1 = vec![4, 1, 2];
let nums2 = vec![1, 3, 4, 2];
println!("{:?}", Solution::next_greater_element(nums1, nums2));
}
// Accepted solution for LeetCode #196: Delete Duplicate Emails
// Auto-generated TypeScript example from py.
function exampleSolution(): void {
}
// Reference (py):
// # Accepted solution for LeetCode #196: Delete Duplicate Emails
// import pandas as pd
//
//
// # Modify Person in place
// def delete_duplicate_emails(person: pd.DataFrame) -> None:
// # Sort the rows based on id (Ascending order)
// person.sort_values(by="id", ascending=True, inplace=True)
// # Drop the duplicates based on email.
// person.drop_duplicates(subset="email", keep="first", inplace=True)
Use this to step through a reusable interview workflow for this problem.
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.
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.
Review these before coding to avoid predictable interview regressions.
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.