I’m preparing for an upcoming software testing interview and I’m having trouble with the manual testing interview questions. I’m a bit rusty on the concepts and could use some help. I’m having trouble understanding the following code snippet:
For (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (a[i] + b[j] == c[i + j])
{
result++;
}
}
}
Can anyone explain what this code is doing? I’m not sure what the purpose of the ‘if’ statement is or how the loop works. Any help would be greatly appreciated.
@rashmi7 : I am not that proficient with this, but i guess : for the first For loop with (int i) it will check for each value of i starting from 0 and ending with n,
i.e: when the loop starts, i will be assigned 0 and then it jumps to the next for loop “j” , where j will be assigned 0 , and then in the if loop , it checks, whether a[0]+b[0]=c[0+0], if yes, the result will be incremented by 1,
then it will go to the for loop with j and increase the value of j to 1, and then check the if statement again, like that, it will check for each value of j starting from 0 to n, while keeping i as 0.
After that the j loop entire cases is executed, the i value changes to 1 and the process repeats again,
by the time , the entire loop is completed, how many times a[i] + b[j] = c [i=j], result will be incremented by that much times, Suppose result is initially 0, and the condition happens 5 times, then result will turn 5 by the end of this .
Also , can I ask you one thing , may i know which company you applied for software testing and may i know are there still openings .
This codes compares entries of 3 lists (a and b to c) and finally increments the result variable.
It combines (by ‘+’) entries of list a (by i) with entries of list b (by j) and compares them (by == ) to an entry of c (by ‘i+j’).
If this is true it adds 1 to result.
If n is the size of the both lists, the code combines all entries of list a with all of list b.
I can be a sub-set.
If I’m right then here are lists / array compared and n refers to the size of the lists.
This start in most programming languages with an index of 0.
e.g. A list with 5 entries has a size of 5 and the indexes go from 0 to 4.
Here’s a hack that you would probably like to help you with basic coding questions.
i.e., Ask Chat GPT to explain you code snippets.
Please be aware of the risks and biases associated with asking an AI for help. The results may vary and be unreliable if there is nothing else available to validate the output – for example, a human with plenty of experience.
I asked it and reviewed it’s answer and it makes sense:
This code snippet is a nested loop that compares elements of three arrays a, b, and c. The first loop iterates over the elements of array a, and the second loop iterates over the elements of array b.
For each pair of elements a[i] and b[j], the code checks whether their sum is equal to the element at index i + j of array c. If this condition is true, then result is incremented.
Here’s a step-by-step explanation of how the code works:
Initialize a variable result to zero.
Start the first loop, which iterates over the elements of array a using an index variable i.
Start the second loop, which iterates over the elements of array b using an index variable j.
Check whether the sum of the elements a[i] and b[j] is equal to the element at index i+j of array c.
If the condition in step 4 is true, increment the result variable by 1.
Repeat steps 3-5 for all pairs of elements in arrays a and b.
Repeat steps 2-6 for all elements in array a.
The final value of result is the number of pairs of elements in arrays a and b whose sum is equal to the corresponding element in array c.
Overall, this code snippet is performing a brute-force search for pairs of elements in arrays a and b that sum to the corresponding elements in array c.
Serious questions: what has this piece of code got to do with manual testing? I would say zero.
It is important you take anything you read with a grain of salt aka with critical mindset. If you ask the interviewer about it and they answer something like “manually testing the source code” (LOL!) then the proper answer is this process is called “code review” and is developer’s job. And if this is a source code of an automation software, then the answer is that it’s part of automated testing and again has nothing to do with manual testing
Anyone got any other idea that I fail to see at the moment?