How to create a random 4 character alphanumeric string

I missed that entirely. You are right. It could have been a trick question.
So, if we changed the question to: Come up with code that generates a 4 character alphanumeric string like: W123, or B235, Z789. Note that special characters are not allowed.
Would the code then be:

object OneTimeCode {
def apply(length: Int = 5) = {
Random.alphanumeric.take(length).mkString("")
}
}

The results of running your code are here under:

  1. apply has an argument 6 - the method returned dNmVwa
  2. apply has an argument 5 - the method returned P049a

Both the results do not contain number.

The way I am interpreting this is: If the interviewer wanted only 4 ‘characters’ or 5 'characters" then the result will contain only characters.

But, if, say, we took the trick part out of the question and assume he just wanted an 4 or 5 length alphanumeric string, then we would need W123 or W1234, right?

And the OneTimeCode code has to be modified, right?

I did not know about the “take” method at all.
Thank you.

Please