Posts

Showing posts with the label 2023-reviewed

Find the maximum number of bomb that can be detonated

  Problem You are given a list of bombs. The range of a bomb is defined as the area where its effect can be felt. This area is in the shape of a circle with the center as the location of the bomb. The bombs are represented by a 0-indexed 2D integer array bombs where bombs[i] = [xi, yi, ri]. xi and yi denote the X-coordinate and Y-coordinate of the location of the ith bomb, whereas ri denotes the radius of its range. You may choose to detonate a single bomb. When a bomb is detonated, it will detonate all bombs that lie in its range.  These bombs will further detonate the bombs that lie in their ranges. Given the list of bombs, return the maximum number of bombs that can be detonated if you are allowed to detonate only one bomb. Input: bombs = [[2,1,3],[6,1,4]] Output: 2 Explanation: The above figure shows the positions and ranges of the 2 bombs. If we detonate the left bomb, the right bomb will not be affected. But if we detonate the right bomb, both bombs will be detonated. So...

LRU cache question

 Problem Design a data structure that follows the constraints of a  Least Recently Used (LRU) cache . Implement the  LRUCache  class: LRUCache(int capacity)  Initialize the LRU cache with a  positive  size  capacity . int get(int key)  Return the value of the  key  if the key exists, otherwise return  -1 . void put(int key, int value)  Update the value of the  key  if the  key  exists. Otherwise, add the  key-value  pair to the cache. If the number of keys exceeds the  capacity  from this operation,  evict  the least recently used key. The functions  get  and  put  must each run in  O(1)  average time complexity. Example 1: Input ["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"] [[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]] Output [null, null, null, 1, null, -1, null, -1, 3, 4] Explanation LRU...
  Given an array of  intervals  where  intervals[i] = [start i , end i ] , merge all overlapping intervals, and return  an array of the non-overlapping intervals that cover all the intervals in the input . Example 1: Input: intervals = [[1,3],[2,6],[8,10],[15,18]] Output: [[1,6],[8,10],[15,18]] Explanation: Since intervals [1,3] and [2,6] overlap, merge them into [1,6]. Example 2: Input: intervals = [[1,4],[4,5]] Output: [[1,5]] Explanation: Intervals [1,4] and [4,5] are considered overlapping.   Constraints: 1 <= intervals.length <= 10 4 intervals[i].length == 2 0 <= start i <= end i <= 10 4

Print a given matrix in spiral form

Image
  Problem Given a 2D array, print it in spiral form. Input:  {{1,    2,   3,   4},               {5,    6,   7,   8},              {9,   10,  11,  12},             {13,  14,  15,  16 }} Output: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10  Explanation: The output is a matrix in a spiral format.  Input: { {1,   2,   3,   4,  5,   6},            {7,   8,   9,  10,  11,  12},           {13,  14,  15, 16,  17,  18}} Expected output: 1 2 3 4 5 6 12 18 17 16 15 14 13 7 8 9 10 11 Explanation : The output is a matrix in a spiral format.

Checking whether interval (a,b) is covered given 1-D list of coordinates

Question Given 1-D list of co-ordinates determine if interval (a,b) is covered Ex - [(2,5), (5,7),(1,4)] and interval = (1,6) return true Explanation - Points 1 to 6 lies in list of interval given 1 to 4. 2 to 5 and 5 to 7. [(1,4),(6,7),(2,5)] and interval - (1,6) return false Explanation - Distance between 5 to 6 is not covered in the list given so return false

Power of 3 problem

Problem Write function to determine if given unsigned 32-bit number is a power of 3     int is_power_of_3(uint32_t n)

Design the BigString class

Problem [Revised] Design the BigString class which has the following public methods // method to add char to the word at the specified index. void insert (char c, int pos) {   // } //method to return the word at the specified index. word * search(int index) { // } Example) insert('a', 0); ==> [a] insert('b', 0); ==> [ba] insert(' ', 0); ==> [ ba] insert('c', 0); ==> [c ba] insert('e', 11) ==> [c ba       e] insert('f', 8); ==> [c ba    f   e] insert('o', 3)  ==> [c boa    f   e] insert('u', 10) ==> [c boa    fu   e] search(0) ==>  c search(1) ==>  boa search(2) ==>  fu search(3) ==>  e

Finding the shortest sequence containing the keywords (Minimum window subsequence problem)

Problem Find the shortest string [] containing the keyword inside Example, Words: sky cloud google search sky work blue Keywords: sky blue Return: sky work blue

Finding the maximum number from two arrays preserving relative order

Problem You are given two arrays of length M and N having elements in the range 0-9. Your task is to create the maximum number of length K from elements of these two arrays such that the relative order of elements is the same in the final number as in the array, they are taken from i.e. If two elements a, and b are taken from array1 and a comes before b in array1 so in the final number a should come before b (Relative order kept same).  Example: N=4 and M =6  Array1 = { 3 , 4, 6,5} Array2 ={9,1,2,5,8,3} Suppose K = 5, then the number will be {9,8,6,5,3} You can see {9,8,3} are taken from Array2 in the same order as they are in Array2. Similarly {6,5} are taken from Array1 in the same order and number 98653 is the maximum possible number.

Picking K matches from N matchbox that makes the sum the minimum multiple of K

Problem Rahul is playing a very interesting game. He has some N different types of matchboxes. All matchboxes may have a different number of matchsticks (S1, S2, S3... Sn). Rahul chooses two random numbers F and K. K should be less than N. The game is that Rahul wants to select any K matchboxes out of N matchboxes such that the total number of matchsticks in these K-selected matchboxes should be multiple of F.  You can only take one patch from each box. No duplicate number is allowed At the same time, Rahul wants the sum of matchsticks of all the selected match boxes should be the minimum possible.  Input Specifications:  1) Array S = {S1,S2,S3,...Sn} of size N corresponding to the number of match sticks in N matchboxes (0<=N<=1000}  2) F-Value (as explained above)  3) K-Value ( as explained above)  Output:  1 2 3 4 5 Here 3 is the number of matchsticks in matchboxes I II III IV V minimum possible ...

Finding the number of ways to climb the stairs - Part 2

Image
Problem You are climbing a stair case. Each time you can either make 1 step or 2 steps. The staircase has n steps. In how many distinct ways can you climb the staircase ? For example, If there are 3 stairs, there are three possible ways to climb them. {1,1,1}, {1, 2}, {2, 1} Your algorithm should print out the total number of ways to climb the stairs. (Optional: should also print out the different ways as above)