Let's say I have a service where I provide user registration. The user chooses a ID. Now if I already have someone who is registered against that ID, I will inform the user that this ID is already used so try something else.

Now I want to provide some suggested IDs to the user based on what information I have while signing up(name, DOB, timestamp etc). How do I provide unique suggestions efficiently?

The obvious idea is to generate some suggestions(like taking user initials and DOB) and then check if they exist in the database, and suggest only those which aren't exisiting. But this is a very inefficient way of doing it I reckon. Is there any algorithm for this? How does something like gmail provide suggestions for email ID to a user while signing up?

As an example, if I am user John Doe and I want to sign up as John, if it is not available then i should get suggestions like J.doe, John.d, JD01(assuming birthdate)

  • 2
    $\begingroup$ It is unusual to ask the DOB before an ID has been provided. You can query your database for all names starting with the attempted login name, followed by digits, and increment the largest value formed by the digits. Alternatively, hash the current time to a short number or short string and check for a collision (several times if needed). $\endgroup$
    – user16034
    Apr 12, 2022 at 8:11
  • $\begingroup$ This sounds good. I agree with the DOB part you said, it was just an example. The reason why I am trying to suggest IDs based on some user input is so that to the user it might seem something meaningful, sort of a better user experience as compared to just appending a random string. Also are there any data structures I can utilise for this purpose, something like a prefix tree? $\endgroup$
    – Rinkesh P
    Apr 12, 2022 at 8:23
  • 1
    $\begingroup$ I think I would see an extra question as unpleasant, if not intrusive. Suggesting some name or letting the user change it seems enough. $\endgroup$
    – user16034
    Apr 12, 2022 at 9:51


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.