Practice Exercise With Solution + *** Interview Question ***

11 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€92.30
List Price:  €129.22
You save:  €36.92
£79.14
List Price:  £110.81
You save:  £31.66
CA$135.92
List Price:  CA$190.30
You save:  CA$54.37
A$153.17
List Price:  A$214.44
You save:  A$61.27
S$134.71
List Price:  S$188.59
You save:  S$53.88
HK$782.26
List Price:  HK$1,095.19
You save:  HK$312.93
CHF 90.56
List Price:  CHF 126.79
You save:  CHF 36.22
NOK kr1,076.34
List Price:  NOK kr1,506.92
You save:  NOK kr430.58
DKK kr688.47
List Price:  DKK kr963.88
You save:  DKK kr275.41
NZ$166.49
List Price:  NZ$233.09
You save:  NZ$66.60
د.إ367.19
List Price:  د.إ514.08
You save:  د.إ146.89
৳11,000.23
List Price:  ৳15,400.77
You save:  ৳4,400.53
₹8,335.05
List Price:  ₹11,669.40
You save:  ₹3,334.35
RM473.25
List Price:  RM662.57
You save:  RM189.32
₦139,656.03
List Price:  ₦195,524.03
You save:  ₦55,868
₨27,872.43
List Price:  ₨39,022.53
You save:  ₨11,150.09
฿3,637.73
List Price:  ฿5,092.97
You save:  ฿1,455.24
₺3,222.80
List Price:  ₺4,512.05
You save:  ₺1,289.25
B$497.67
List Price:  B$696.75
You save:  B$199.08
R1,888.37
List Price:  R2,643.79
You save:  R755.42
Лв180.67
List Price:  Лв252.95
You save:  Лв72.27
₩134,942
List Price:  ₩188,924.20
You save:  ₩53,982.20
₪366.06
List Price:  ₪512.51
You save:  ₪146.44
₱5,625.18
List Price:  ₱7,875.48
You save:  ₱2,250.29
¥15,118.78
List Price:  ¥21,166.90
You save:  ¥6,048.12
MX$1,663.06
List Price:  MX$2,328.35
You save:  MX$665.29
QR365.44
List Price:  QR511.63
You save:  QR146.19
P1,373.02
List Price:  P1,922.28
You save:  P549.26
KSh13,148.68
List Price:  KSh18,408.68
You save:  KSh5,260
E£4,723.53
List Price:  E£6,613.14
You save:  E£1,889.60
ብር5,694.53
List Price:  ብር7,972.57
You save:  ብር2,278.04
Kz83,612.73
List Price:  Kz117,061.17
You save:  Kz33,448.44
CLP$98,024.19
List Price:  CLP$137,237.79
You save:  CLP$39,213.60
CN¥722.49
List Price:  CN¥1,011.52
You save:  CN¥289.02
RD$5,928.03
List Price:  RD$8,299.48
You save:  RD$2,371.45
DA13,469.95
List Price:  DA18,858.47
You save:  DA5,388.52
FJ$225.25
List Price:  FJ$315.36
You save:  FJ$90.11
Q781.84
List Price:  Q1,094.62
You save:  Q312.77
GY$20,987.69
List Price:  GY$29,383.60
You save:  GY$8,395.91
ISK kr13,817.61
List Price:  ISK kr19,345.21
You save:  ISK kr5,527.60
DH1,010.57
List Price:  DH1,414.84
You save:  DH404.26
L1,763.81
List Price:  L2,469.40
You save:  L705.59
ден5,677.01
List Price:  ден7,948.05
You save:  ден2,271.03
MOP$807.66
List Price:  MOP$1,130.76
You save:  MOP$323.09
N$1,895.98
List Price:  N$2,654.45
You save:  N$758.46
C$3,689.21
List Price:  C$5,165.04
You save:  C$1,475.83
रु13,357.88
List Price:  रु18,701.57
You save:  रु5,343.68
S/371.02
List Price:  S/519.45
You save:  S/148.42
K383.60
List Price:  K537.05
You save:  K153.45
SAR375.02
List Price:  SAR525.04
You save:  SAR150.02
ZK2,548.47
List Price:  ZK3,567.96
You save:  ZK1,019.49
L458.83
List Price:  L642.38
You save:  L183.55
Kč2,334.38
List Price:  Kč3,268.22
You save:  Kč933.84
Ft36,425.52
List Price:  Ft50,997.18
You save:  Ft14,571.66
SEK kr1,059.50
List Price:  SEK kr1,483.35
You save:  SEK kr423.84
ARS$85,666.43
List Price:  ARS$119,936.42
You save:  ARS$34,269.99
Bs695.09
List Price:  Bs973.16
You save:  Bs278.06
COP$389,673.99
List Price:  COP$545,559.18
You save:  COP$155,885.18
₡50,509.65
List Price:  ₡70,715.54
You save:  ₡20,205.88
L2,474.33
List Price:  L3,464.17
You save:  L989.83
₲736,638.54
List Price:  ₲1,031,323.43
You save:  ₲294,684.88
$U3,781
List Price:  $U5,293.55
You save:  $U1,512.55
zł398.17
List Price:  zł557.46
You save:  zł159.28
Already have an account? Log In

Transcript

Hello guys, welcome back to the next tutorial. In this one, I'm going to go over a homework that I created for you guys. And it's a very common interview questions as well for QA engineers, let's go over it quickly. So the exercises that you need to reverse the string order, so assume that you have a string and I have a sample string as this is a testing unit to write a code to reverse the string. Basically, the output should be string test a is this. So if you notice carefully, the order is reversed string is here on the first place.

And this came at the last place, but string word is still intact, didn't come in the first place and then go and the last place and all those sort of things, so you just need to be careful about that. The other part that I want to talk about is there is a reverse method built and reverse method in Java. So please do not use it because you want to implement the knowledge that you have learned in previous lectures. And you want to utilize a lot of things That I've written here, you're going to utilize, if conditions as conditions, call loops, and some built in functions, not the dot reverse one. But other built in functions like dot blade, dot trim, and all those sort of things. Other important thing that you want to do is you want to do some text for input string, because we want to validate that the input string is valid, like this sample string that I'm taking, right, we want to validate if this is valid.

In terms of that, if I am not providing it, let's say I'm just passing a empty string or another string to the method, then I want to check for that thing. So because that's what we expect from the developer side, that's where the bugs come in. So you just want to be careful about those things, just do some text and make your code kind of bulletproof right, it's gonna really help your coding skills. So for now, you guys can just stop the video, pause the video or, and then just resume back when you're ready with the code and or whenever you are whenever you're ready to see the solution. So I'm just gonna go over and close this document I now bring up Eclipse I'm going to go with this solution. So what I have is a reverse String class that's like, that's what I created and a main method.

In this I have an input string variable in which I have a simple string, right. And then I have a reverse method that I'm calling with the input. And I'm taking it in the output, and I'm just printing out the output. So it should just print the reverse of the string, right. And then this reverse method is declared up here. And it just takes one input.

And there is one reverse variable string that I declared, it's right now empty. So double quotes with nothing is empty string. And if I put one spaces as well, it's not an empty string, it takes a space and space is a character. So don't misunderstand with the empty string. So anything in the double quote with nothing, it's an empty string. So this is an empty string right now.

And this is what I'm returning as the reverse and in between, we will add the logic. So let's get started with logic. So let's first assume that we want to see if the length of the string is good, greater than one, right? Because let's say if my string is justice a, and this is my string, so what is going to be the reverse of a, it's just going to be a right. So in that case, if the length of the string is one only, I want to return the same string back. So we want to check for those things.

And we want to continue afterwards. So let's say if the input dot length is kind of less than or equal to one, then we want to do that in my reverse. Reverse is a placeholder where we're gonna put our reverse string, right, so in my reverse, I'm gonna put input right away. And then we're done with the code for that part, right? Then later, we want to handle the situation when the input is greater than or greater than one, right? So you can have n equals two equals to one as well because anything less than one is zero.

And zero length is empty, and we're going to handle it differently. So where it's just good to have this way. And in the else part, what we're gonna do is, we're going to take the approach that we want to put all these words in an array. And we want to use dot split function of Java to put all these into an array. And then we want to access the array, add them back to our reverse string variable in the reverse order. So that's how we're going to get the reverse string, right.

So let's create a string array. I'm just gonna call it original array, you guys can go over the a lecture to, again, brush up these things. And then I'm gonna say input dot split. So you guys can see it takes the regex to split, and it just returns the array out of the string. So I'm just going to provide two inputs, my input string, which is actually this in the regex. I'm going to provide backslash backslash two first is the escape sequence.

And the second is for regex. So it should be in the double quotes. backslash backslash, s ss for the space. So it's going to split the string on spaces. And but it's going to fail if I have multiple spaces in between words. So for that, what I want to do is I'm gonna put a plus sign here, which is the regular expression question for that.

So please feel free to explore some regular expressions as well. And this is going to split the string, even if doesn't matter how many strings are there in between the two words. So I'm going to put a semicolon up there, it's kind of split the string and put it in the original array. So then afterwards, what we can do is we can just loop over the original array, and everything in this original array is string type. I'm gonna take a string item, a variable, and I'm going to loop over the original array. And this is a for each loop.

So there's a lecture for for loops. Just feel free to go over it to brush up the knowledge again, if you're confused in this one, and after that we're gonna do is we're gonna add the reverse we want to this is a placeholder where we will put everything. So in this reverse, we will do item, this is the item plus and after that we want to just add the reverse, right. So this is all we want to do. And let's see why we're getting this error. It says it doesn't find the original area because we have a type of there already.

So now it finds it. So there is now we have item trust reverse. How it works is, so the first time when it accessed the item, it's going to access the variable. So the item this right because in the array, the item is going to be like this. This sorry, let's put the comments. This is a Test string.

So this is the array, right? Zero index first index, second, third and fourth. Right? So the first time it's going to access this, it's going to access this. And it's going to add reverse, which is empty string. So basically, in this reverse variable, we're going to have this only right?

So after that in the second iteration, it's going to access this item is going to access is, right, so it's going to add this plus reverse. And what do we have in reverse? We have this in reverse, right? So we want to have this and penetration, it's going to access a and plus reverse. And what do we have in reverse? Now we have is this in reverse, so plus is gone.

It's just an addition that I was showing in the last step. It's going to add this Is this right? And then it's going to have test plus a This, right and in the last iteration is going to access thing. Plus test is this. So finally we'll have string test is this in our reverse variable. So it allows, we can just return the reverse variable.

And to be sure that we don't have any leading or trailing spaces, we can just put a trim up here. So what does trim function does is it just removes any leading or trailing whitespace from a string variable. So this is our code, and it should work pretty good. So let's run it. And see string test is this. So now what's the issue with this because they're just adding item and reverse.

To make it exactly in the reverse order, what we should do is we should just add a space in both of them, right? So when we add the space and we run the code, again, we will say string test is this. That's the desired output that we wanted, right? So here's the achievement achievement number one, the second achievement we want to do is we want to handle the input types. support that, that's come in, in the top we want to take my input that is empty, basically, if this is empty, are, so again, we are utilizing a condition that is empty, a lot of things are my input is equals to null. In that case, what I want to do is I just want to give a message to the user right?

Then I'll say something like empty and null strings are non accepted. I can just say this, right? Something like that. So if I pass in, like this kind of string, which is empty, and I run the code, it's gonna say an empty and those strings are not accepted right and it's a zero length. So we are good to go with this. Right.

So We have done a lot of tests that could be a lot more successful. So I leave it to you guys to practice to figure out if there's something else that you can think of. So what we have is we are checking for empty string, which you can call null string, where prompting the user that no dude, this is not accepted. And then we're checking for the length, it's only a single character, we just hit on the same input. If it's not a single character, it's more than one characters, we take it in the array, we split it with the spaces, we iterate over the array, and then we add it in the reverse order. So this was again or rephrase of everything that we did.

After that, we captured everything in the reverse string variable, and then we return the reverse dot trim. So we will have everything up here in the output. And here we are printing the output. So that's pretty much it guys. And I would say I I'm pretty sure you guys must have achieved this one. So practice as always, guys.

Because that's the only thing that you're going to learn. And otherwise no practice, your hands will be still be kind of struggling to tie the code. So, practice as much as you can. And stay tuned for the next lectures guys. Thanks for attending this one.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.