Posts

Showing posts with the label Difficulty:Medium

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.

Finding possible permutation of N words [reviewed]

Problem Permutate a list of string  this question is supposed to permutate the characters instead of the whole string,  Here is input example {"red", "fox", "super" } the expected output is  r fs, rfu, rfp, rfe, rfr, ros, rou, rop, roe, ror, rxs, rxu, rxp, rxe, rxr, efs, efu, efp, efe, efr, eos, eou, eop, eoe, eor, exs, exu, exp, exe, exr, dfs, dfu, dfp, dfe, dfr, dos, dou, dop, doe, dor, dxs, dxu, dxp, dxe, dxr

Parsing the php code

Problem Given a tokenized PHP file, give me a map from class to list of functions. Here is same PHP code. class Foo {     public $aMemberVar = 'aMemberVar Member Variable';     public $aFuncName = 'aMemberFunc';     function aMemberFunc() {         print 'Inside `aMemberFunc()`';     } function bob() { print "hi, Hi am functopm bob2 "; } } class Olivia { function say() { print "hi"; } } $foo = new Foo; class bogus ; Write the code to return the map of the class name to the list of functions.

Managing the conference rooms with balancing the occupancy percentage

Problem Given the list of conference rooms with the capacity and the target occupancy percentages, write the program that maintains the balanced occupancy percentage across the rooms. Input: list of rooms { [30, 0.6], [50, 0.7], [90, 0.4]}, number of guests Output: list of rooms with balanced occupancy percentages across the rooms

Reordering the values in the binary tree.

Problem Given the tree as below, write the code that fixes the order of the tree such that the value of the parent is always larger than the value of the children (left, right) struct node {     int value;     node *left;     node *right;     node(int v):value(v),right(0),left(0){) } Input: root node                2             /    \            5       10           / \     /  \          4   3   8    9                    / \      /  \                 12  6  1   11 The expected result will be:            12       ...

Decoding numeric string to alphabets.

Image
Problem Given a string of integers returns the number of ways to decode integers back to Alphabets using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 examples: input = 199 output = 2 explanation: 1-> 9 -> 9 (AII) 11-> 9 (KI) input = 11 output = 1 explanation: 1->1 (AA) 11 (K)

Finding the shortest path from start to end word given list of words

Given list of array and start and end word as below:    ["hot", "hit", "his", "hat"] Start word: "hot" End word: "his" Find the shortest sequence starting the from start and ending the with end word. The rule is, from the start word, each time you can only change one character, and only use words from the given set, find out how to get to the end word. In the above example, the sequence is:    hot  -> hit -> his (hat is not used) for this example: start = 'his' end = 'hit' input = ["his", "has", "hit", "hat"] we can find this: his -> has -> hat -> hit. However: his -> hit is the sorted path.

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

Sorting the strings in 20GB file with one string in each line.

Problem Imagine you have a 20 GB file with one string per line. Explain how you would sort strings in the file.

Finding the magic index in array, A

Image
Problem A magic index in an array A [0. . .n-1] is defined to be an index such that A[i] = i. Given a sorted array of distinct integers, write a method to find a magic index, if one exists, in array A. FOLLOW UP What if the values are not distinct?

Getting the total covered length given the interval

Problem Write the program that takes the series of intervals and returns a total length covered by the added intervals.  If several intervals intersect, the intersection should be counted only once.  Example:  addInterval(3, 6)  addInterval(8, 9)  addInterval(1, 5)  getTotalCoveredLength() =>; 6  I.e. [1,5) and [3,6) intersect and give a total covered interval [1,6).       [1,6) and [8,9) don't intersect, so the total covered length is a sum of both intervals, that is 5+1=6.                     ______                                        _           _________          0  1  2  3  4  5  6  7  8  9  10      Now, implement the follow...

Sorting the patient's files with three types: Low, Med, High

Problem   An efficient way to sort patient files in an array of just 3 types, "high-importance', 'med-importance', 'low-importance', which are in an arbitrary order(unsorted)   The output preference should start with the highest.   example: [high, low, low, med, high, low]

Shuffling the deck of cards perfectly randomly.

Problem Write the program to shuffle the deck of cards in statistically random order.

Planting flowers with no adjacent flower plots

Image
Problem Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adjacent plots - they would compete for water and both would die. Given a flowerbed (represented as an array containing booleans), return if a given number of new flowers can be planted in it without violating the no-adjacent-flowers rule  Sample inputs  Input: 1,0,0,0,0,0,1,0,0  3 => true  4 => false  Input: 1,0,0,1,0,0,1,0,0  1 => true  2 => false  input: 0  1 => true  2 => false  public boolean canPlaceFlowers(List flowerbed, int numberToPlace) {  // Implementation here  }

Searching the number in the phone book

Problem If I type some numbers in my cell, all phone numbers which have these typed numbers in any order should appear, tell data structure for this.  Example: if I type 926 then  932678....  92678...  9777726....  should appear.  Let me clear it through another example  eg: i enter 321, then  o/p(if they are in book)  9344241..  972153....

Placing N queens in N x N Chess board

Image
Problem In $N \times N$  chess board, you have to arrange N queens such that they do not interfere each other. Following is how you define interference of queens.  1. Two queens cannot be on the same diagonal  2. Two queens cannot be in same horizontal or vertical line  3. Queen can jump like a knight. So, two queens cannot be at a position where they can jump two and half steps like a knight and reach the other queen.  You should return the possible ways to arrange N queens on a chess board. 

K way merge problem

Image
Problem Assume you have very  K large arrays of integers stored in the Disk. It is so large that I would not fit into your computer memory. You need to write the application that merge all K large arrays into sorted one list and store it in the disk. Example, say the memory size, K = 6. Your program should be able to merge the following three arrays. Note that each file has sorted list of integers.   File 1  = { 3 , 7 , 9 , 15 , 35 };   File 2  = { 2 , 8 , 45 , 60 , 70 , 100 };   File 3  = { 5 , 6 , 50 , 101 , 110 }; You need to write a program that merges the arrays into one array with all the numbers sorted in ascending order.