Hi everyone, let's do this question.
You are given a string consisting of only lowercase and uppercase English alphabets and integers 0 to
9.
All right, write a function that will take this string as input and return the index of the first character
that is non-repeating.
All right.
Now when you're doing this in an interview, feel free to ask clarifying questions so that you have
a thorough understanding of what the question is talking about.
Now, a possible question is you could ask, can you tell me what do you mean with first non-repeating
character?
Let's say this was under stress.
And, uh, immediately you were not able to grasp the meaning of first non-repeating character.
So there's no problem.
You can always ask a clarifying question.
And let's say the interviewer responds, the first character that occurs only one time.
All right.
So you get a good understanding.
Now you could ask another question.
What if it's not there?
What if in the string there is no non-repeating character?
Now the interviewer could say, okay, in that case just return null.
Okay.
And another question that you could ask is because over here you have lowercase and uppercase English
alphabets.
If A and a occurs small a and capital A occurs, should it be treated as repeating?
And let's say the interviewer responds no.
All right.
So at this point we have developed a good understanding of what the question is asking.
Now let's proceed and write out some test cases.
Again.
Remember test cases are just various inputs and what the expected output is in those scenarios.
And you can always ask the interviewer, is it okay if we come up together with some test cases for
the question?
So that is always a good practice.
Now let's go ahead and write a few test cases.
If the string that's given to us is this 1AAB, c one AC bd.
All right.
Now in this case what should be the output.
We need the index of the first non-repeating character.
Now we can see that A is repeating over here.
So this is not non-repeating but this small A is not repeating.
So yes this is the first non-repeating character.
And the index of this will be one.
Right.
So the output of the function has to be one.
So this is 0123 etc..
Right.
So we are picking this one and that should be the output.
What if the given string is AAAA33.
So there is no non-repeating character in this given string.
So in this case as per the clarification that we received the output has to be null.
Now what if the given string is a b a?
In this case, the first non-repeating character is this one because we treat these two differently.
And the index over here is zero.
So that should be the output.
All right.
So we have got a good understanding of the question.
And we have written some test cases.
And both the interviewer and you are on the same page.
You have good clarity.
Now let's proceed and think about ways in which we can solve this question.