Problem · Array

Phone Numbers by Last Name

Learn this problem
EasyGrubhub logoGrubhubFULLTIMEONSITE INTERVIEW

Problem statement

You are given an array of contact records and a requested lastName.

Each record is a valid string in one of these forms:

  • first last, 111-222-3333
  • first middle last, 111-222-3333

Name tokens are separated by one space, and the comma is followed by one space. Return every phone number whose final name token exactly equals lastName.

Matching is case-sensitive. Preserve input order and preserve duplicate phone numbers.

Function

phoneNumbersByLastName(records: String[], lastName: String) → String[]

Examples

Example 1

records = ["Ada Lovelace, 111-222-3333","Grace Brewster Hopper, 222-333-4444","Byron Lovelace, 333-444-5555"]lastName = "Lovelace"return = ["111-222-3333","333-444-5555"]

The first and third records have the exact last name Lovelace, so their phone numbers are returned in input order.

Example 2

records = ["Alex Kim, 111-111-1111","Alex kim, 222-222-2222"]lastName = "Kim"return = ["111-111-1111"]

Matching is case-sensitive, so kim does not equal Kim.

Example 3

records = ["A B C, 999-000-1111"]lastName = "Z"return = []

No record has the requested last name, so the result is empty.

Constraints

  • 1 <= records.length <= 10000.
  • Each record contains either two or three nonempty alphabetic name tokens and a phone number in the shown format.
  • Each name token has length from 1 through 30.
  • 1 <= lastName.length <= 30, and lastName is alphabetic.

More Grubhub problems

drafts saved locally
public String[] phoneNumbersByLastName(String[] records, String lastName) {
    // write your code here
}
records["Ada Lovelace, 111-222-3333","Grace Brewster Hopper, 222-333-4444","Byron Lovelace, 333-444-5555"]
lastName"Lovelace"
expected["111-222-3333", "333-444-5555"]
checking account