Posts

Showing posts from 2023

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) Updates the price of

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 the maxi

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 LRUCache lRUCache = new LRUCache(2); lRUCache.put(1,
  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.