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: Users
+---------------+---------+ | Column Name | Type | +---------------+---------+ | user_id | int | | name | varchar | | mail | varchar | +---------------+---------+ user_id is the primary key (column with unique values) for this table. This table contains information of the users signed up in a website. Some e-mails are invalid.
Write a solution to find the users who have valid emails.
A valid e-mail has a prefix name and a domain where:
'_', period '.', and/or dash '-'. The prefix name must start with a letter.'@leetcode.com'.Return the result table in any order.
The result format is in the following example.
Example 1:
Input: Users table: +---------+-----------+-------------------------+ | user_id | name | mail | +---------+-----------+-------------------------+ | 1 | Winston | winston@leetcode.com | | 2 | Jonathan | jonathanisgreat | | 3 | Annabelle | bella-@leetcode.com | | 4 | Sally | sally.come@leetcode.com | | 5 | Marwan | quarz#2020@leetcode.com | | 6 | David | david69@gmail.com | | 7 | Shapiro | .shapo@leetcode.com | +---------+-----------+-------------------------+ Output: +---------+-----------+-------------------------+ | user_id | name | mail | +---------+-----------+-------------------------+ | 1 | Winston | winston@leetcode.com | | 3 | Annabelle | bella-@leetcode.com | | 4 | Sally | sally.come@leetcode.com | +---------+-----------+-------------------------+ Explanation: The mail of user 2 does not have a domain. The mail of user 5 has the # sign which is not allowed. The mail of user 6 does not have the leetcode domain. The mail of user 7 starts with a period.
Problem summary: Table: Users +---------------+---------+ | Column Name | Type | +---------------+---------+ | user_id | int | | name | varchar | | mail | varchar | +---------------+---------+ user_id is the primary key (column with unique values) for this table. This table contains information of the users signed up in a website. Some e-mails are invalid. Write a solution to find the users who have valid emails. A valid e-mail has a prefix name and a domain where: The prefix name is a string that may contain letters (upper or lower case), digits, underscore '_', period '.', and/or dash '-'. The prefix name must start with a letter. The domain is '@leetcode.com'. Return the result table in any order. 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":{"Users":["user_id","name","mail"]},"rows":{"Users":[[1,"Winston","winston@leetcode.com"],[2,"Jonathan","jonathanisgreat"],[3,"Annabelle","bella-@leetcode.com"],[4,"Sally","sally.come@leetcode.com"],[5,"Marwan","quarz#2020@leetcode.com"],[6,"David","david69@gmail.com"],[7,"Shapiro",".shapo@leetcode.com"]]}}Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1517: Find Users With Valid E-Mails
// Auto-generated Java example from py.
class Solution {
public void exampleSolution() {
}
}
// Reference (py):
// # Accepted solution for LeetCode #1517: Find Users With Valid E-Mails
// import pandas as pd
//
//
// def valid_emails(users: pd.DataFrame) -> pd.DataFrame:
// pattern = r"^[A-Za-z][A-Za-z0-9_.-]*@leetcode\.com$"
// mask = users["mail"].str.match(pattern, flags=0, na=False)
// return users.loc[mask, ["user_id", "name", "mail"]]
// Accepted solution for LeetCode #1517: Find Users With Valid E-Mails
// Auto-generated Go example from py.
func exampleSolution() {
}
// Reference (py):
// # Accepted solution for LeetCode #1517: Find Users With Valid E-Mails
// import pandas as pd
//
//
// def valid_emails(users: pd.DataFrame) -> pd.DataFrame:
// pattern = r"^[A-Za-z][A-Za-z0-9_.-]*@leetcode\.com$"
// mask = users["mail"].str.match(pattern, flags=0, na=False)
// return users.loc[mask, ["user_id", "name", "mail"]]
# Accepted solution for LeetCode #1517: Find Users With Valid E-Mails
import pandas as pd
def valid_emails(users: pd.DataFrame) -> pd.DataFrame:
pattern = r"^[A-Za-z][A-Za-z0-9_.-]*@leetcode\.com$"
mask = users["mail"].str.match(pattern, flags=0, na=False)
return users.loc[mask, ["user_id", "name", "mail"]]
// Accepted solution for LeetCode #1517: Find Users With Valid E-Mails
// Rust example auto-generated from py reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (py):
// # Accepted solution for LeetCode #1517: Find Users With Valid E-Mails
// import pandas as pd
//
//
// def valid_emails(users: pd.DataFrame) -> pd.DataFrame:
// pattern = r"^[A-Za-z][A-Za-z0-9_.-]*@leetcode\.com$"
// mask = users["mail"].str.match(pattern, flags=0, na=False)
// return users.loc[mask, ["user_id", "name", "mail"]]
// Accepted solution for LeetCode #1517: Find Users With Valid E-Mails
// Auto-generated TypeScript example from py.
function exampleSolution(): void {
}
// Reference (py):
// # Accepted solution for LeetCode #1517: Find Users With Valid E-Mails
// import pandas as pd
//
//
// def valid_emails(users: pd.DataFrame) -> pd.DataFrame:
// pattern = r"^[A-Za-z][A-Za-z0-9_.-]*@leetcode\.com$"
// mask = users["mail"].str.match(pattern, flags=0, na=False)
// return users.loc[mask, ["user_id", "name", "mail"]]
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.