Posts

Stock price processing problem

Question You are given a stream of records about a particular stock. Each record contains a timestamp and  the corresponding price of the stock at that timestamp. Unfortunately due to the volatile nature of the stock market, the records do not come in order. Even worse, some records may be incorrect.  Another record with the same timestamp may appear later in the stream   correcting the price of the previous wrong record. Design an algorithm that: Updates the price of the stock at a particular timestamp, correcting the price from any previous records at the timestamp. Finds the latest price of the stock based on the current records. The latest price is the price at the latest timestamp recorded. Finds the maximum price the stock has been based on the current records. Finds the minimum price the stock has been based on the current records. Implement the StockPrice class: StockPrice() Initializes the object with no price records. void update(int timestamp, int price) U...

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.

Find the shorted path from the vertex 0 for given list of vertices.

Question For input (separated by a tab) from this link , compute the shortest path from a vertex, 0 to the following vertices. The file contains an adjacency list representation of an undirected weighted graph with 200 vertices labeled 1 to 200.  Each row consists of the node tuples that are adjacent to that particular vertex along with the length of that edge. For example, the 6th row has 6 as the first entry indicating that this row corresponds to the vertex labeled 6. The next entry of this row "141,8200" indicates that there is an edge between vertex 6 and vertex 141 that has a length of 8200.  The rest of the pairs of this row indicate the other vertices adjacent to vertex 6 and the lengths of the corresponding edges. Report the shortest path in the same order of given vertices vertices = [7,37,59,82,99,115,133,165,188,197] if you find that all ten of these vertices except 115 are at a distance of 1000 away from vertex 1 and 115 is 2000 distances away, then your answer s...

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

Pattern matching through regular expression [reviewed]

Problem Pattern Matching  ----------------  Characters: a to z  Operators: * +  * -> matches zero or more (of the character that occurs previous to this operator)  + -> matches one or more (of the character that occurs previous to this operator)  Output if a given pattern matches a string.  Example:  pattern:a*b  string:aaab b, ab, aab, aaab, ab  output:1  pattern:a+aabc  string:ab aabc, aaabc, aaaabc ..  output:0  pattern:aa*b*ab+  string:aab aab, aabab, aaaabbab  output:1  pattern: a+a*b*  string: a ab, aab, aaabb  output: 1  Valid Assumptions: Please assume that both the pattern and string input are valid

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.

Finding the minimum steps to reach {m,n} in the grid [reviewed]

Problem A robot has to move in a grid which is in the form of a matrix. It can go to  1.) A(i,j) --> A(i+j,j) (Down)  2.) A(i,j)-->A(i,i+j) (Right)  Given it starts at (1,1) and it has to go to A(m,n), find the minimum number of STEPS it has to take to get to (m,n) and write  public static int minSteps(int m,int n)  For instance to go from (1,1) to m=3 and n=2 it has to take (1, 1) ->(1, 2) ->(3, 2) i.e. 2 steps

Finding the first missing number given the list with numbers [reviewed]

Image
Problem Given input [2,3,1,5,88,100], find the first missing number. All number are positive numbers. (1 - n) In the above example, 4 is the first missing number.

Make the perfect balance on the balances in the room [reviewed]

Image
You have a room-full of balances and weights. Each balance weighs ten pounds and is considered perfectly balanced when the sum of weights on its left and right sides are exactly the same. You have placed some weights on some of the balances, and you have placed some of the balances on other balances. Given a description of how the balances are arranged and how much additional weight is on each balance, determine how to add weight to the balances so that they are all perfectly balanced. There may be more than one way to balance everything, but always choose the way that places additional weight on the lowest balances. The input file will begin with a single integer, N, specifying how many balances there are. Balance 0 is specified by lines 1 and 2, balance 1 is specified by lines 3 and 4, etc... Each pair of lines is formatted as follows: WL WR WL and WR indicate the weight added to the left and right sides, respectively. is a space-delimited list of t...

Implement the read4 method [reviewed]

Problem Implement read using read4 class ArbitraryIO { private :     // returns bytes read or 0, sizeof(buf) >= 4     // reads up to 4 bytes at a time into caller allocated buf     int read4( char * buf); public :   // IMPLEMENT:toRead > 4 or < 4 or = 4, buf allocated by caller, return number of bytes read   int read( char * buf, size_t toRead) {   } }; Your method should pass the following test cases: ------ test case  --- // assume reader is reading over ->  a b c d e f g h i j ArbitraryIO r; char buf[ 1024 ]; int x; x = r.read(buf, 2 ); EXPECT(x == 2 ); EXPECT(buf[ 0 ] == 'a' ); EXPECT(buf[ 1 ] == 'b' ); x = r.read(buf, 5 ); EXPECT(x == 5 ); EXPECT(buf[ 0 ] == 'c' ); EXPECT(buf[ 1 ] == 'd' ); EXPECT(buf[ 2 ] == 'e' ); EXPECT(buf[ 3 ] == 'f' ); EXPECT(buf[ 4 ] == 'g' ); x = r.read(buf, 1024 ); EXPECT(x == 3...

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

Guessing the word from a dictionary [reviewed]

Problem Assuming you're playing one game that you need guess a word from a dictionary. You're given a machine you can try to guess the word, the machine will return how many characters has been matched by your guess. Design a system to crack the word.

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

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)