Given a text file file.txt
that contains a list of phone numbers (one per line), write a one-liner bash script to print all valid phone numbers.
You may assume that a valid phone number must appear in one of the following two formats: (xxx) xxx-xxxx or xxx-xxx-xxxx. (x means a digit)
You may also assume each line in the text file must not contain leading or trailing white spaces.
Example:
Assume that file.txt
has the following content:
987-123-4567 123 456 7890 (123) 456-7890
Your script should output the following valid phone numbers:
987-123-4567 (123) 456-7890
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for finding valid phone numbers involves checking every possible combination of numbers to see if it matches the required phone number pattern. We essentially try out all the possibilities, one by one, until we find the ones that are valid.
Here's how the algorithm would work step-by-step:
def find_valid_phone_numbers_brute_force(input_string):
valid_phone_numbers = []
string_length = len(input_string)
for start_index in range(string_length):
for end_index in range(start_index + 1, string_length + 1):
possible_phone_number = input_string[start_index:end_index]
# Check if the current substring is a valid phone number
if is_valid_phone_number(possible_phone_number):
remaining_string = input_string[end_index:]
# If the remaining string is empty, we have a valid phone number
if not remaining_string:
valid_phone_numbers.append(possible_phone_number)
else:
# Recursively check the rest of the string for more valid numbers
remaining_valid_numbers = find_valid_phone_numbers_brute_force(remaining_string)
# If the remaining string can be broken down into valid numbers, save
if remaining_valid_numbers:
for number in remaining_valid_numbers:
valid_phone_numbers.append(possible_phone_number + ' ' + number)
return valid_phone_numbers
def is_valid_phone_number(phone_number):
# Check if the number matches a simple phone number pattern
phone_number = phone_number.replace('-', '').replace(' ', '')
if len(phone_number) == 10 and phone_number.isdigit():
return True
elif len(phone_number) == 7 and phone_number.isdigit():
return True
else:
return False
To efficiently determine if a list of strings are valid phone numbers, we'll focus on checking each string against a specific pattern rather than comparing every string to each other. We'll use a shortcut that quickly confirms if a string matches the required format, skipping any further checks if it does not.
Here's how the algorithm would work step-by-step:
def valid_phone_numbers(phone_numbers):
valid_numbers = []
for phone_number in phone_numbers:
# Check the phone number against the expected format.
if len(phone_number) == 14 and \
phone_number[0] == '(' and \
phone_number[4] == ')' and \
phone_number[5] == ' ' and \
phone_number[9] == '-':
is_valid = True
# Validate each character within the number is a digit
for i in range(1, 4):
if not phone_number[i].isdigit():
is_valid = False
break
for i in range(6, 9):
if not phone_number[i].isdigit():
is_valid = False
break
for i in range(10, 14):
if not phone_number[i].isdigit():
is_valid = False
break
if is_valid:
valid_numbers.append(phone_number)
return valid_numbers
Case | How to Handle |
---|---|
Null or empty input array | Return an empty boolean array to indicate no phone numbers to validate. |
Phone number strings with length less than 10 or greater than 14 | Return false for these numbers immediately as they cannot match the required format. |
Phone number strings with non-digit characters in the digit positions | The validation logic should check each character is a digit in the correct position, returning false otherwise. |
Phone number strings with incorrect delimiters (e.g., using '[' instead of '(') | The validation logic should strictly check for the presence and correct placement of '(', ')', and '-'. |
Very large input array with many phone numbers | The solution's time complexity should be O(N) where N is the number of phone numbers, avoiding quadratic or worse performance. |
Phone number strings with leading or trailing whitespace | Trim the whitespace from the phone number string before validation, ensuring accurate matching. |
Array contains phone numbers that are close to valid but have minor deviations (e.g. (123)1234-567 or (123) 123-4567) | The validation must strictly enforce the 'XXX) XXX-XXXX' format, returning false if deviations are found. |
Phone numbers where all digits are same (e.g. (111) 111-1111) | The validation should not have any special treatment for these cases; it should validate the format regardless of the digits' values. |