Find Products with Three Consecutive Digits - Problem

You are given a table Products that contains product information. Your task is to find all products whose names contain exactly three consecutive digits.

Key Requirements:

  • The sequence must be exactly three digits in a row
  • Products may have multiple such sequences, but each qualifying sequence must be exactly 3 digits
  • Return results ordered by product_id in ascending order

Example: 'ABC123XYZ' contains '123' (3 consecutive digits), while 'Product56789' contains '56789' (5 consecutive digits, so it doesn't qualify).

Table Schema

Products
Column Name Type Description
product_id PK int Unique identifier for each product
name varchar Product name that may contain digits and letters
Primary Key: product_id
Note: Each row represents a unique product with its ID and name

Input & Output

Example 1 — Mixed Product Names
Input Table:
product_id name
1 ABC123XYZ
2 A12B34C
3 Product56789
4 NoDigitsHere
5 789Product
6 Item003Description
7 Product12X34
Output:
product_id name
1 ABC123XYZ
5 789Product
6 Item003Description
💡 Note:

Pattern Analysis:

  • ABC123XYZ contains exactly 3 consecutive digits: 123
  • A12B34C has digits but not 3 consecutive ones
  • Product56789 has 5 consecutive digits (too many)
  • NoDigitsHere has no digits
  • 789Product starts with exactly 3 consecutive digits
  • Item003Description contains exactly 3 consecutive digits: 003
Example 2 — Edge Cases
Input Table:
product_id name
1 12
2 1234
3 A123B456C
4 000
Output:
product_id name
3 A123B456C
4 000
💡 Note:

Edge Case Analysis:

  • 12 - only 2 digits (too few)
  • 1234 - 4 consecutive digits (too many)
  • A123B456C - contains two separate groups of exactly 3 digits each
  • 000 - exactly 3 consecutive digits (leading zeros count)

Constraints

  • 1 ≤ product_id ≤ 1000
  • name contains only alphanumeric characters
  • Product names can be up to 50 characters long

Visualization

Tap to expand
Find Products with Three Consecutive Digits INPUT ID Product Name 1 Widget123Pro 2 Gadget45 3 Tool789Set 4 Part1234Unit 5 Item567Box 6 Device89 Products table with various digit patterns Exactly 3 digits Not 3 digits ALGORITHM STEPS 1 Use REGEXP Pattern Match [0-9]{3} pattern 2 Exclude 4+ Digits NOT REGEXP [0-9]{4} 3 Filter Results Keep only exact matches 4 Order by ID ASC sorting SELECT * FROM products WHERE name REGEXP '[0-9]{3}' AND NOT '[0-9]{4}' ORDER BY product_id FINAL RESULT ID Product Name 1 Widget 123 Pro 3 Tool 789 Set 5 Item 567 Box Rejected: Gadget45 (only 2 digits) Part1234Unit (4 digits) Device89 (only 2 digits) 3 Products Found Key Insight: The regex pattern [0-9]{3} matches any sequence of exactly 3 consecutive digits. To ensure EXACTLY 3 (not more), we must also exclude strings matching [0-9]{4} or more. This two-condition approach guarantees we find only products with precisely three consecutive digits. TutorialsPoint - Find Products with Three Consecutive Digits | Optimal Solution
Asked in
Amazon 23 Microsoft 18 Meta 15
23.4K Views
Medium Frequency
~8 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen