Hi everyone, let's do this question.
You are given an array of integers and another integer target value.
Write a function that will take these inputs and return the indices of the two integers in the array
that add up to the target value.
All right, now let's thoroughly understand the question first.
And remember in the interview when this is actually happening with an interviewer, feel free to ask
clarifying questions so that you and the interviewer are on the same page, and that you have sufficient
understanding about the question to be able to solve it.
Now, in this question, it says that we are given an array of integers and we are given one more integer,
and we it's called the target value.
And we need to check whether any two elements in the array which is given to us add up to the target
value, and if so, then we need to return the indices of those two integers in the array.
So that's the question.
All right.
Now let's simulate the interview by asking a few clarifying questions.
And these are some of the questions that you could ask.
Now let's say you're asking the interviewer what if no two integers add up to the target value.
So what should you do at that point?
Now let's say the interviewer replies, okay.
In that case, return an empty array.
All right.
Now, another question which you could ask is will multiple pairs of integers add up to the target value?
And let's say the interviewer says no.
At most one pair will add up to the target value.
Now again you could ask are all numbers positive?
Now the interviewer could say no, there can be negative integers also.
Now don't worry, it's not exactly required that probably this question may or may not have an impact
on the solution, but you can feel free that if it's a valid question, if you feel that it's good to
know, you can feel free to ask that question to the interviewer.
And again, another question could be are all the integers distinct?
And probably the interviewer says, yes, all the integers are distinct.
And another question that you could ask is what if the array of integers that is given as an input is
empty?
Now the interviewer says at this point, if this is the case, then return an empty array.
All right.
And again you ask, can I add the same integer twice to get the target value.
And the interviewer replies at this point, no.
All right.
So these are some of the clarifying questions that you could ask for this question.
So always feel free to ask sufficient clarifying questions to get a good understanding about the question.
Now once you have done this, let's work on some test cases.
Remember, test cases are just different inputs and you write out the expected output in those cases
and in the actual interview you can form these test cases along with the interviewer.
So that's not a problem.
You can always say all right.
So thank you for the answers that you have given.
Now is it okay if we work together to formulate some test cases so you can do that.
All right.
Now let's say the input array is two comma seven.
And the target value given is nine.
So what should be the output in this case.
In this case the output has to be zero comma one.
Right.
Because the indices over here this this has an index zero and this has an index one.
And we need to return the indices as per the question.
Now what if the target.
What if the array which is given to us is let's say 273 minus one and four.
And the target value is equal to two.
Now in this case we can see that if we add up these two three and minus one we will get the target value.
So we need to return the indices of these two values.
So this is 012.
This is two and this is three.
Right.
So the answer or the output in this case should be two comma three.
Now what if again the same array is given as input.
And the target value let's say is equal to 100.
So there are no two values in the input array which add up to the target value.
So in this case the output has to be an empty array as per the clarification that we got from the interviewer.
And let's say you have a single element as input which is 25 and the target value is also 25.
So what should be the output in this case?
In this case also the output has to be an empty array because as per the question, we need to find
two integers that add up to the target value.
So again only one integer is there in this array.
So that's not possible.
And we have to return an empty array.
And let's say again an empty array is given as an input.
And whatever target value is given in this case also the output has to be an empty array.
All right.
So we have understood the question and we have formulated test cases.
Now it is time to think of a way to solve this.
So we will look at two ways in which we can solve this.
Now before you go ahead and look at the solution, try to think through the problem and try to think
about a way in which you would solve this.