Hello
Recently someone asked to come up with Scala code on an over the phone coding screen. I failed that interview of course. That is the bitter truth.
So this question here is only to educate myself on how to solve that problem. Perhaps it might just help me learn more about Scala . That is the intention.
Stressful as it was and the fact that I had to come up with a one-liner Scala expression quickly, did not help.
I forgot about scala.util.Random and I had totally forgotten about java’s Random class’s getInt().
’
So, the question was: Come up with code that generates a 4 character alphanumeric string like: W1234, or B2351, Z7891. Note that special characters are not allowed. Capitalization does not matter.
I could go into the scala,util.Random library and try writing the code myself. But I would like to request the Scala folks here to help me solve this problem, from scratch. I want to get a glimpse into the thought process of a seasoned, experienced, quick-thinking Scala programmer.
My goal is to learn from all this and start contributing my own solutions on this forum. Let this be the way to get all this started.
If you want a specific pattern you’d need to adjust accordingly, and it’s worth noting that this doesn’t use SecureRandom so it’s not cryptographically strong.
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:
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.
I ran your code again by passing argument 5
This time, it generated, xW0L4.
What if we wanted our string to be exactly of the pattern: W1234, a letter followed by 4 numbers.
This may not be necessarily what the had interviewer wanted. I am just trying to challenge myself
Mildly related - never use screen shots of code. Copy the text instead. For me, someone using a screenshot to show code is a huge red flag, right there.
scala> OneTimeCode.apply
:10: error: missing arguments for method apply in object OneTimeCode;
follow this method with `_’ if you want to treat it as a partially applied function
OneTimeCode.apply
^
You’ll find that some interviewers intentionally ask confusing, vague, or even incorrect questions, to see how you respond when given unclear requirements.
So seems to be the case. I thought something was not right about the interviewer’s question but, did not step up and could not show my chops, speaking of which, I have some building to do.
The f string interpolator uses C-style printf format strings (printf - Wikipedia), in which %04d means to left-pad an integer with 0s until it’s at least 4 full digits.
printf format strings are 1970s stuff. I am not seriously suggesting that involving one is the best way to go here, I just thought it would be fun to throw a variety of solutions out there.