TopCoder problem "UserId" used in SRM 164 (Division I Level One)

Problem Statement

    Our system has thousands of users, each with a unique User Id. When a new user needs an id, we want to assign his User Id based on his first, middle, and last name.

We do not want to assign a User Id if the names were incorrectly entered. If the names are unreasonable we will return "BAD DATA". A name is judged to be unreasonable if it contains a character other than a letter ('a'-'z' or 'A'-'Z'), an apostrophe ''', or a space character ' '. It is also unreasonable if the first or last name contains less than 2 letters ('a'-'z' or 'A'-'Z').

The algorithm for choosing a User Id is to choose the first rule below that produces a User Id that has not already been assigned to another user. In each case, truncate the last name as needed so that the resulting User Id is no longer than 8 characters.

  • [first initial][last name]
  • [first initial][middle initial][last name]
  • [first initial][last name][digit][digit]

Before we form the User Id, we will simply eliminate all apostrophes and space characters, and will change all uppercase letters to lowercase. If there is no middle name (or if the middle name contains no letters) then we must skip the second rule. When we apply the third rule, we will try the digit pairs in order starting with 01 then 02 etc. until we find a previously unused User Id.

Create a class UserId that contains method id that takes a String[] inUse containing all the User Id's that have already been assigned and Strings first, middle, and last as input. It will return "BAD DATA" if the name data is unreasonable, or will return the User Id generated by our algorithm.



Parameters:String[], String, String, String
Method signature:String id(String[] inUse, String first, String middle, String last)
(be sure your method is public)


-inUse will contain between 0 and 50 elements inclusive
-each element of inUse will contain between 3 and 8 characters inclusive
-each character in each element of inUse will be a lowercase letter 'a'-'z' or a digit '0'-'9'
-first, middle, and last will each contain between 0 and 50 characters inclusive
-first, middle, and last will contain only ascii characters 32-126 inclusive


Returns: "bjones01"
Rule 1 generates "bjones" which is already taken. Rule 2 does not apply when there is no middle name. Rule 3 generates "bjones01", which is not already taken.
 "Bob Mack"
Returns: "bhjones"
Rule 1 generates "bjones" which is already taken Rule 2 generates "bhjones" which is available.
Returns: "BAD DATA"
The hyphen in "BoB-Mack" is illegal.
"'m m"
Returns: "momorr02"
The name is just barely reasonable because the first name has just the two letters required, and the middle name is allowed to be empty. Rule 1 generates "momorris", which is already in use. Rule 2 cannot be applied since there is no middle name. So rule 3 is applied.
"'m m"
Returns: "BAD DATA"
The period in the middle name is not allowed.

Problem url:

Problem stats url:




lbackstrom , brett1479

Problem categories:

Simulation, String Manipulation