Suppose there is a robot on an infinite grid that starts at point (0, 0). It is facing towards north direction. Now the robot can receive one of three possible types of commands −-2 to turn left 90 degrees-1 to turn right 90 degreesany value from 1 to 9 to move forward x unitsThere are some grid squares that are obstacles.We also have another array called obstacle, this indicates the i-th obstacle is at grid point (obstacles[i][0], obstacles[i][1]), If the robot wants to move onto them, the robot stays on the previous grid square instead.We have to find the square ... Read More
Suppose we have a positive integer N, we have to find the longest distance between two consecutive 1's in the binary representation of N. If there are no two-consecutive 1's, then return 0.So, if the input is like 22, then the output will be 2, as 22 in binary is 10110. There are three ones In the binary representation of 22, and two consecutive pairs of 1's. The first consecutive pair of 1's have distance 2, then the second consecutive pair of 1's have distance 1. Answer will be the largest of these two distances, which is 2.To solve this, ... Read More
Suppose there is a lemonade stand, each lemonade costs $5. Now customers are standing in a queue to buy from the store, and order one at a time.Each customer can buy only one lemonade and pay with either a $5, $10, or $20 bill. We have to must provide the correct change to each customer, so that the net transaction is that the customer pays $5. And at first, we have no change in hand.We have to check whether we can provide every customer with correct change.So, if the input is like [5, 5, 5, 10, 20], then the output ... Read More
Suppose we have two strings A and B of lowercase letters; we have to check whether we can swap two letters in A so that the result equals to B or not.So, if the input is like A = "ba", B = "ab", then the output will be True.To solve this, we will follow these steps −if size of A is not same as size of B, thenreturn Falseotherwise when A and B has any element that are not common, thenreturn Falseotherwise when A is same as B and all characters are distinct in A, thenreturn Falseotherwise, count:= 0for i ... Read More
Suppose we have a grid we have to find the number of magic square sub-grid into that grid. A magic square is a 3 x 3 grid filled with distinct numbers from 1 to 9 such that each row, column, and both diagonals all have the same sum.So, if the input is like438495192762then the output will be 1, as the magic square is438951276To solve this, we will follow these steps −Define one set with values: [816357492, 834159672, 618753294, 672159834, 492357816, 438951276, 294753618, 276951438]Define an array offset of size: 9 x 2 := {{-2, -2}, {-2, -1}, {-2, 0}, {-1, -2}, ... Read More
Suppose there is a rectangle that is represented as a list [x1, y1, x2, y2], where (x1, y1) is the coordinates of its bottom-left corner, and (x2, y2) is the coordinates of its top-right corner. Now two rectangles overlap if the area of their intersection is positive. So, we can understand that two rectangles that only touch at the corner or edges do not overlap.If we have two (axis-aligned) rectangles, we have to check whether they overlap or not.So, if the input is like R1 = [0, 0, 2, 2], R2 = [1, 1, 3, 3], then the output will ... Read More
Suppose we have a binary matrix A, this is the representation of an image, we want to flip the image horizontally, after that invert it, and finally return the resulting image. To flip the image horizontally each row of the image will be reversed. And to invert the image each 0 will be replaced by 1, and each 1 will be replaced by 0.So, if the input is like110101000then the output will be100010111To solve this, we will follow these steps −result:= a new listfor each row i in A, doReverse:= reverse row ifor j in range 0 to size of ... Read More
Suppose there is a string S of lowercase letters, these letters form consecutive groups of the same character. So, when a string like S is like "abbxxxxzyy" has the groups "a", "bb", "xxxx", "z" and "yy". A group will be a large group when it has 3 or more characters. We would like the starting and ending positions of every large group.So, if the input is like "abcdddeeeeaabbbcd", then the output will be [[3, 5], [6, 9], [12, 14]]To solve this, we will follow these steps −ans := a new listcsum := 0for each a, b in make group of ... Read More
Suppose we have a list of points on a plane. We have to find the area of the largest triangle that can be formed by any 3 of the points.So, if the input is like [[0, 0], [0, 1], [1, 0], [0, 2], [2, 0]], then the output will be 2To solve this, we will follow these steps −res := 0N := size of points listfor i in range 0 to N - 2, dofor j in range i + 1 to N - 1, dofor k in range i + 2 to N, do(x1, y1) := points[i], (x2, y2) ... Read More
Suppose we have a string S and we have to write the letters of that given string, from left to right into lines. Here each line has maximum width 100 units, and if writing a letter would cause the width of the line to exceed 100 units, that will be written on the next line. We also have an array widths, here widths[0] is the width of 'a', widths[1] is the width of 'b'and so on.We have to find the answers of two questions −How many lines have at least one character from SWhat is the width used by the ... Read More