Transcript for:
Pages Networks Coding Questions

so these three are the coding questions asked in the internet broadcasting round of pages networks the first question was let's read the problem statement of first question it's clearly a physics based normal physics-based formula question just we need to have the basic knowledge of physics or is it just the formula is only given we just need to reduce out on the relationship works and we need to calculate here and the F1 F2 denotes the force of uh by applied by second person first person and second person respectively and D1 and P2 denotes the displacement applied by for for first person and second person respectively so we can calculate the work done by first person work done by second person and reduce minus it from the complete total overall work and and the and the subtraction will be the result and the assumption is here that the overall work is always greater than the uh the resultant work it is all you can see here it is given the work is always greater than the multiplication of force into displacement so and also one is there that the resultant obtained is always will be in the range of integer value so let's work to see the test cases the network is given the final work is given 50 and Force 1 is given f 1 5 and displacement is 2 Force 2 is 7 displacement is three so resultant as a net work done is 50 minus Force into displacement of further work and for a portion displacement of first person plus the force and displacement applied by second person so 5 into 10 plus 7 into 321 the answer is 50 minus 31 answer is nineteen okay same goes here 50 plus 6 and we'll add that and subtract it from 155 and the resultant will be the answer so let's implement this function this was very basic question and yes of here I am coding it in Eclipse that's why I need to uh here I need to come I need to write the main method and all but as you will be putting in the console that they will be providing you only need to write the method that I am writing here the code written below the method here so let's calculate the work done by first person so the work one is clearly states question the Force One into displacement one course one into displacement one and the work done by second person is W2 is F1 into D1 work 2 is F2 into D2 so the work done by first person work done by second person we have obtained it will subtract it from the network so Network then so network is Network equals to the overall work is w o r k minus work done by first person that is W1 plus W2 this was a basic question if you will see the test cases also you will understand how code works and their explanation is given in the question it was very well understood from the explanation that how it will work so just this four line of code let's see in the main method that our code runs for the given test cases or not so we will calculate call this for this method work lab so we'll pass the respective test case now respective data like 50 it is clearly given 50 comma 5 comma 2 comma 7 comma 3 so answers it should generate the answer 19 so let's check it it is generating the answer 19 okay so this code is correct now we'll see for the second test cases it is 155 155 15 6 10 and 4. 10 and 4 it should generate for its original 25 as a result yeah it is generating so the code is correct and this code has passed all the test cases I am zooming it if you want to see the code okay you can see it now I'm moving to the second question question two question two clearly states that we have here we have two truck truck a and drug p the coordinates of track a is x coordinate is ax and the p x coordinate is a y so uh a a y of a a truck and the same goes with track B the x coordinate is b x and per y b y as input and the location of office is at 0 0 origin and we need to calculate that which uh distance from the office is nearest for which term okay track a is nearer to origin origin or the office or truck V is uh nearer to office so that we need to calculate and there is two assumptions are given or two nodes are given no need to calculate the square root of distance between them just and this formula will also work so it is it is also given that clearly hint is given that only you need to calculate this formula by putting the value into of ax and a y into this and if the drugs are of equal distance you need to return either of any distances okay so let's code for the method they have asked let's copy it again you do and there you don't don't have to write the question you don't have to write the main method and all you just need to write this letter okay see so let's calculate the value so int first let's calculate the first distance in D1 so we can calculate D1 as you can see here x square into y Square so we will calculate it ax into ax ax into ax Plus V A Y plus a y into a y so we will get the distance of T1 we'll get the distance of in T2 we can how we can regulate D2 like x square is given so p x is the distance coordinate of coordinate of second track so p x into BX plus we are just I am putting the variable name in comparison with the formula our static just you can see just a basic question of common sense if you will apply you will get this and you will see the discussion now we have to return the minimum of the square distance that which truck is nearer to the destination so we can write here int our result is equals to math dot mil you can also apply here you can also apply here if else condition if you want but for not writing a longer put I am doing this D1 and D1 code so the video becomes no longer that's why I am writing this short course now I will return this result I you have understood what is I have I think you will have understood what is as it was one of the basic question written result r u t u r n okay 10th result okay let's see it passes all the test case or not we'll call this method for the data distance and will Part 12 comma 5 comma 12 comma 9 it should generate the answer as we can see 160 9. foreign next portion is 12 into 12 is what 12 and 12 is 144 but 9 into 9 is what 81 so 144 plus 81 gives I think 255 225 sorry so we then and 225 is stored in D2 it calculates the result of method minimum of 169 and 225 it and their value is stored in result okay so let's check this also for the second test cases then we will generate a random test cases for us so that we can see that it also works for the given node that if both the distances are equal it should generate any one of the results so we'll check for that that our code works correctly for that code or not answer should be should be 25 so let's check it yeah correct either this code has also passed successfully passed all the test cases in examination now let's check it for any random distance that gives equal output okay 6666 so D1 is also 36 D2 is also 36 so it is giving that any one return square of any disk either of the drug I so let's see it's ended it generates with which of 72. 66 and result of okay let's run it again I don't know why it is given 60. uh okay 6 into 6 is 36 plus 36 okay yeah 72 exactly exactly 6 into 6 is 36 plus 3672 D1 also stores 72 D2 also scores 72 and the result can be any of the value so the answer is 72 it all successfully worked for this test case also okay now again I am zooming it if you want to copy this if you want to see this code you can see okay yeah you see this one okay let's move to question number three option number three was one of the standard question it was related to link list one of the question of linked list so you will see here in the question I have created three methods linked list in which I've created the liquids method and a node class and a main method so in order to explain this question I need to create I have created this all message so that my linked list and all get created but in the console that you will get in the question you don't have to create this all you need just need to write the methods here the uh here you have to give here you have been given a simply linked list with head of the node points to the first element of the linked list and you need to calculate the sum of the digits and generate the new linked list with their sum like you can see here uh there here is two catches also given that same size and the single digit should only be present at the data position of that each node so okay so let's see the question is 2 4 3 first single legal list is 243 second leg list is five six four okay and you will see the second uh so addition of 4 plus 3 is 7 and edition of 6 plus 4 is tens one is carry 5 plus 2 7 8 8 1 got passed there and the answer is this okay so it was very simple question now you will if you will see here in the next question what about if there is one extra carry here you will see seven four eleven one and six two eight one is nine and eight five thirteen thirteen is not stored at same location it's five what I've said only single digit can be present at the each data location okay so every approaching I will be following a very simple method you will see this question this type of question um a lot of places like gfg on all but I won't use such tricks I would just do iteration for first link list iteration for second click list I will iterate for second in English I will literate for first grade let's generate the number I will iterate for secondly let's generate the number I will add that number like normal editions and then I will I will pass the edit added value and create a single link list and we will represent this result okay if you will do it with iteration it will become more tougher question will become more difficult okay I will follow the basic approach to solve this question so let's I will be creating a digit method okay but that will return the digits of the link list okay let's create a method there is static in digits in digit State and type will be what the number that the linked list has been the datas of all the English that that it contains okay so let's no name it as node node okay now okay return type is in type exactly so let's do this now I will create a variable in dig that will store the digits of linked list past so let's process to it where we create a temp variable that will store the head of node that is node and will iterate it through throughout the until the linked list not gets over okay until temp is not equal sooner we will what will do what we'll add that number or data to our dig variable so pemp dot data P dot data okay now we'll increase temp request to Temp dot next this is all the iteration for extracting the digits for forming a digit this was basic iteration I will return the digit r-a-t-u-r-n d i g okay I will show you that what this code generates this is a method okay now you'll see what I will do I will call this method digit method in the mail just in order to display you what have informed so let's do in num1 that is it will as its statement type is number one so I will call the method digits for what linked list one linked list one dot head it will I will pass the head of first link list I will pass the head of your truck and Link list for the second data to be generated digits parallel two dot head okay parallel two dot head now you will see here what key the number will be this linked list data will be stored in the form of basic number here like uh if you want I if you want to see this let's print the value of num1 and number one it's for you I will show you so that you can understand more but in a better way so let's see let's run the loop which is entering the data for first Nicholas so first click list here is 2 4 3 okay so plus 2 4 and 3 will control yes I need to next it enter is 2 4 yes three no I don't want to enter more data so enter the data for second request it is five six and four five yes six yes or yes no so answer it what you can see and that 243 is formed in the form of number and 564 is number and I have stored this in the form of variable now I will do what I will create a normal variable control set I will create a variable that will store the sum of these two and then I will create the linked list from the for each digit of that sum okay because very basic approach that I've followed I know there is some problem with this approach I will deal with it later I'll explain you what are the problems and how this questions can be made more complex so you uh no more complex to you so I have stored this in the form of variable now if I will uh I will store this I will and display the sum my sum variable will store the sum of 2 4 3 and 5 6 for that is 4 plus 3 7 6 plus 4 10 0 a 5 plus 2 8 0 eight zero seven it will store okay now I need to create the linked list I need to create the linked list from the data given and I need to display it so I will pass my sum variable okay so I will create uh first of all I will create I will call it create method create linked list I will create a method create linked list that will return a node type node type is what it will be return the head of the linked list okay static node linked list and it I will pass for I will pass the variable in p i g t i g is here is the sum variable there is some variable that we are using somewhere even so let's create that variable and let's name it as value I will be passing the sum of the two numbers that I have generated from the two respective link list so let's create a link list uh static node create LL enter value okay yeah correct parameter display note head is equals to null I have created a dummy variable head that points to null I am extracting the last digit as you know the extractions begins from left hand side from right hand side so I will extract the right hand side that I will extract here is three this is sum of some whatever will be the sum and there digits from the right hand side okay so in dig is equals to Value mod 10 and again value equals to Value python I am extracting each series and reducing the number by each by each digit okay so this was a basic code I know you have understood this node P equals to new node I am creating the new node that and I will put the data P dot data P dot data is uh P dot data is DIC I have extracted the digit in dig okay and then V Dot next is null the next is null I've created a node extracted I've created a node as you can see that node node method consists of data and next so load data has been assigned with the digit and next is assigned to null okay now I will make this as it is the first node will create the head of the new linkery list okay so head is p now I will create a loop while value is greater than 0 for more value this was the first digit for the first rightmost digit and if more digits are there for that we are zip writing this code we are writing dig same code dig let's copy this no that's right p i g is equal to value not 10 okay and where it will extract the digit and reduce it by one digit okay okay now so let's function here we will create a new variable node Q equals to new node from the for the digit that we have extracted now Q dot net Q dot data restore the data whatever we extracted that is gig Q dot data is dig Q dot next is null Q dot next is null okay so we'll create the next is null and P dot next is Q will make the P dot next means previous variable next is Q so the link list is all about links so we are generally creating the previous joining the previous node link to this queue so the head node is connected to the next digit because we need to display it also on the on the form of links or we have to return it return a link so we need to create have the links in the links or Incarnation with each other okay so return head you can see we will return head return type is head so it Returns the ad okay so return head is here we have done now let's what we did to do so we can now what we do we can in the main method you can call the method for this node Z is equals to we have created a method create linked list and we'll pass the sum variable some variable to it so create link list and pass just some variables some variable is this it will be fine just so now I have written the code to create the click list so let's visualize the link let's start it is a correct or not so we'll create a method that will print the linked list okay that the either our link list is correct or not okay so let's create a method as fast as possible static value static sorry static void print okay let's name it print link list and we'll pass the node add variable to it for the root of root of the linked list head variable of the link list so now we'll create a temp variable so that our head doesn't got lost temp equals to head navigate the entire loop on this temp is not equals to null temp equals to Temp I will print it okay we'll print it so we'll print it system.out.print temp dot data data okay so let's say okay let's store the temp dot data and will increase the time equals to Temp dot next this question was very lengthy so they have given 45 uh two easy questions for first and second questions okay so this to print the link list now you can see receiver run this you will see what we you haven't expected okay I will I will run this code to show you something like two yes four yes three no I have inputed the first take this now I am entering the second language is five yes six yes four four no okay we will see okay uh the latest form I haven't passed I haven't written here I need to call the printing list method in order to print it foreign and we need to pass ahead of the note that off the node so I have passed ahead of the node okay so let's let's run this again to show you some magic or whatever result you haven't expected press 4 yes 3 2 4 3 yes no so enter the date of second decrease that is 5 yes text yes or no okay see what happens answer should be eight zero seven but what it did that it I always said that extraction of digits begin from right hand side so it did the same thing the addition was formed correctly as I have written on Sony previously Edition was correct but when this creative method was called the evaluated value mod 10 was obtained eight zero seven the result about eight zero seven but the extraction will be first digit from the right hand side extraction was first digit was seven created it made the seven as head and so on it get good God on joining The Links of first node with Earth next and next and so on similarly I understood this is what is happening here what's happening here so in order to you can see a simple logic in order to prevent this from happening we just if if what we will reverse our linked list so our work will be easier or what will be easier what so that is the result we want so just you write a simple code for reversing the link list your work is done so let's write this code for reversing the link list and our source or whatever result we want we'll be we'll get whatever result we will want so let's create a very static node reverse and we'll pass the head to pass the head so let's don't write in verse method so node previous is equals to none current equals to head see linked list is all about visualization if you will visualize things you will get how that each whatever code I have written is purely logical if you will think about this you will get get each and every point so nicely that you that you will never forget you don't need to memorize it it's all about Logics here current time currency and T current is not equals to null or until current is not equals To None we need to run this code current is not equal to 1 next is equals to next cross current we are pointing to making the current to next element current next element next dot or current dot next sorry that's not not written current dot next okay now I need to write current next is see this is very basic code of reversing I think if you are solving this question you must be knowing having the idea of linked list and this put basic code of reversing iteration and all if you have this much knowledge you will get this question very nicely and yeah I'm really saying that this question can be made more complex very very complex I will state your in the last that how you can make this question very very complex in the phone they can make this question very complex in the interview by adding only few changes in the in the test cases current is next okay so this is the code of Just previous is made current current is made next and head is made that is what previous and will return type is return head ER head just this method is done for reversing so that this 807 is not printed seven zero it is not printed eight zero seven is printed now see in the main method what we are doing we are printing the linked list before printing if we will call this method this also will display the same output but we need to reverse it so we need to call the reverse method and pass the head variable to it and then we need to call the create linked list will be created reverse will be method will be calculated so the links with on the head will point to now 8 and we will get the correct output so let's check our code runs successfully or not to S4 yes three input in the wrong format two yes I want to enter more data oh yes I want to input more data 24 and 3 yes no I don't want to input more data okay no now let's for checkpoint yes for the second we are inputting the data forces and neglectrics clearly in the console it is showing five yes uh yeah I want to enter five six yeah five six and four four no so okay you can see somewhere I made the mistake somewhere I made the mistake while let's check the reverse method right because it was turning says fully before it current dot next current dot next equals to previous previous equals to current current equals to current equals to next third equals to previous return head okay clearly the code is correct let's see in the main method if I have made some mistake so in the main method before printing the link list I need to call reverse method I uh yeah you know see it will this reverse method is also what this method reverse method is also passed return this reverse method has some return type it is making returning the nude head so you need to store it because before printing we need to have the new head not the previous head okay so that was a mistake we are creating so let's start turn this our code for one again once more to four yes two four yes three no okay so let's enter the date of second click this that is five yes I want to enter more six yes five six and four five six and four yeah now I don't want to get more answer should be eight zero and seven yeah see this is what you want so let's check for second test case second test case is five first yes six yes seven no let's check for second eight two and four press enter eight yes two yes four no answer is what one three nine one so you can clearly see that this code is giving the correct output here so our code is successful our code responsible all the test cases but see I said you that this question can be made very complex Now by adding such a modifications I will now say you what all modifications they can make like they can say that what about if the tool linked lists are unequal then if two legalists are unequal if the first linked list is part of the second link list is an equal means I can either the first click list is greater or secondly stimulator like this understood but the first degree is greater than secondary list and you need to make respective changes according to it because five can't be added with two because three need to be added with 4 and 400 into 4 and 2 is added to be six then whatever it carries where it need to be passed to 5 okay this can be and when more modifications can be made what about if it has more data like 12. 12 okay if it will having weird digit greater than 10 then this digit function will be extracting each digit for for this also 12 it will run for two times or if it is greater than uh tenths or three so you need to make certain modifications for that code also so this quality just has two basic assumptions I said the legally star of same size and the r is only single digits data will only contain a single digit but this can be made a very complex question or very tricky question by adding certain modifications that I stated by changing the size of the linked list by by changing the data digits nothing not only present single digits greater than single digits can also be present and the non-single digits can also be present then you need to write the another code for that that will be a very difficult question you can see that this code was a big question that why they give 45 minutes to solve three question so that's all if you like the video if you like it like it and subscribe the channel that's all thank you have a nice day bye