fork download
  1. /**
  2.  * PROBLEM: Price at Query Timestamps
  3.  * FOLLOW-UP 1: Queries are sorted in ascending order
  4.  *
  5.  * Given:
  6.  * - times[i]: timestamp at which prices[i] starts applying.
  7.  * - prices[i]: price associated with times[i].
  8.  * - queries[i]: timestamp at which we need to find the price.
  9.  *
  10.  * For each query q, find the price corresponding to the
  11.  * largest timestamp that is less than or equal to q.
  12.  *
  13.  * EXAMPLE:
  14.  * times = [0, 50, 500]
  15.  * prices = ["A", "B", "C"]
  16.  * queries = [-10, 20, 50, 499, 600]
  17.  *
  18.  * Output: ["", "A", "B", "B", "C"]
  19.  *
  20.  * ASSUMPTIONS:
  21.  * - times is strictly increasing.
  22.  * - queries is sorted in non-decreasing order.
  23.  * - times.length === prices.length.
  24.  * - A price applies from its timestamp until the next
  25.  * price record takes over.
  26.  * - Return "" if the query is before the first timestamp.
  27.  * - Multiple queries can have the same timestamp.
  28.  *
  29.  * APPROACH: Two Pointers
  30.  *
  31.  * - Since both times and queries are sorted, we don't need
  32.  * to perform binary search for every query.
  33.  *
  34.  * - Maintain a pointer i in the times array, representing
  35.  * the index of the latest valid price record.
  36.  *
  37.  * - Initialize i = -1 because initially we haven't found
  38.  * any valid price record.
  39.  *
  40.  * - For every query, advance i as long as the next
  41.  * timestamp is less than or equal to the query.
  42.  *
  43.  * - Since queries are sorted, pointer i never needs to
  44.  * move backward.
  45.  *
  46.  * - If i === -1, no price is available for the current query.
  47.  * Otherwise, prices[i] is the answer.
  48.  *
  49.  * PATTERN: Two pointers / Simultaneous traversal of sorted arrays
  50.  *
  51.  * TIME: O(n + q)
  52.  * - n = number of price records.
  53.  * - q = number of queries.
  54.  * - The outer loop processes q queries.
  55.  * - The inner while loop advances i at most n times
  56.  * across ALL iterations of the outer loop.
  57.  * - Therefore, the complexity is O(n + q), NOT O(n * q).
  58.  *
  59.  * SPACE:
  60.  * - Auxiliary space: O(1)
  61.  * - Output space: O(q)
  62.  */
  63.  
  64. function priceAtTimes(times, prices, queries) {
  65. const res = [];
  66.  
  67. // Index of the latest valid price record.
  68. // -1 indicates that no valid price has been found yet.
  69. let i = -1;
  70. // Use i + 1 below instead of i.
  71. for (const query of queries) {
  72.  
  73. // Advance the pointer as long as the next timestamp
  74. // is within bounds and is a valid candidate.
  75. //
  76. // We check times[i + 1] because i already represents
  77. // the latest valid record found so far.
  78. while (
  79. i + 1 < times.length &&
  80. times[i + 1] <= query
  81. ) {
  82. i++;
  83. }
  84.  
  85. // At this point, i represents the largest index
  86. // whose timestamp is less than or equal to query.
  87. //
  88. // If i is still -1, no valid price record exists.
  89. res.push(i === -1 ? "" : prices[i]);
  90. }
  91.  
  92. return res;
  93. }
  94.  
  95.  
  96. // ---------------- TEST HARNESS ----------------
  97.  
  98. const tests = [
  99. // Test 1: Normal case covering multiple price intervals
  100. {
  101. times: [0, 50, 500],
  102. prices: ["A", "B", "C"],
  103. queries: [-10, 20, 50, 499, 600],
  104. expected: ["", "A", "B", "B", "C"]
  105. },
  106.  
  107. // Test 2: Queries exactly match recorded timestamps
  108. {
  109. times: [5, 12, 30],
  110. prices: ["low", "mid", "high"],
  111. queries: [5, 12, 30],
  112. expected: ["low", "mid", "high"]
  113. },
  114.  
  115. // Test 3: All queries before the first timestamp
  116. {
  117. times: [10, 20, 30],
  118. prices: ["A", "B", "C"],
  119. queries: [-10, 0, 5, 9],
  120. expected: ["", "", "", ""]
  121. },
  122.  
  123. // Test 4: All queries after the last timestamp
  124. {
  125. times: [10, 20, 30],
  126. prices: ["A", "B", "C"],
  127. queries: [31, 50, 100],
  128. expected: ["C", "C", "C"]
  129. },
  130.  
  131. // Test 5: Multiple queries within the same price interval
  132. {
  133. times: [10, 50, 100],
  134. prices: ["A", "B", "C"],
  135. queries: [11, 20, 30, 49],
  136. expected: ["A", "A", "A", "A"]
  137. },
  138.  
  139. // Test 6: Duplicate query timestamps
  140. {
  141. times: [5, 10, 20],
  142. prices: ["A", "B", "C"],
  143. queries: [5, 5, 10, 10, 20],
  144. expected: ["A", "A", "B", "B", "C"]
  145. },
  146.  
  147. // Test 7: Single price record
  148. {
  149. times: [100],
  150. prices: ["only"],
  151. queries: [99, 100, 1000000000],
  152. expected: ["", "only", "only"]
  153. },
  154.  
  155. // Test 8: Pointer must skip multiple price records
  156. // Important: This is why we need a while loop, not an if.
  157. {
  158. times: [10, 20, 30, 40, 50],
  159. prices: ["A", "B", "C", "D", "E"],
  160. queries: [10, 50],
  161. expected: ["A", "E"]
  162. }
  163. ];
  164.  
  165. tests.forEach(({ times, prices, queries, expected }, index) => {
  166. const actual = priceAtTimes(times, prices, queries);
  167.  
  168. const passed =
  169. JSON.stringify(actual) === JSON.stringify(expected);
  170.  
  171. console.log(
  172. `Test ${index + 1}: ${passed ? "PASS" : "FAIL"}\n` +
  173. `Times: ${JSON.stringify(times)}\n` +
  174. `Prices: ${JSON.stringify(prices)}\n` +
  175. `Queries: ${JSON.stringify(queries)}\n` +
  176. `Expected: ${JSON.stringify(expected)}\n` +
  177. `Actual: ${JSON.stringify(actual)}\n`
  178. );
  179. });
Success #stdin #stdout 0.03s 5712KB
stdin
Standard input is empty
stdout
Test 1: PASS
Times: [0,50,500]
Prices: ["A","B","C"]
Queries: [-10,20,50,499,600]
Expected: ["","A","B","B","C"]
Actual: ["","A","B","B","C"]

Test 2: PASS
Times: [5,12,30]
Prices: ["low","mid","high"]
Queries: [5,12,30]
Expected: ["low","mid","high"]
Actual: ["low","mid","high"]

Test 3: PASS
Times: [10,20,30]
Prices: ["A","B","C"]
Queries: [-10,0,5,9]
Expected: ["","","",""]
Actual: ["","","",""]

Test 4: PASS
Times: [10,20,30]
Prices: ["A","B","C"]
Queries: [31,50,100]
Expected: ["C","C","C"]
Actual: ["C","C","C"]

Test 5: PASS
Times: [10,50,100]
Prices: ["A","B","C"]
Queries: [11,20,30,49]
Expected: ["A","A","A","A"]
Actual: ["A","A","A","A"]

Test 6: PASS
Times: [5,10,20]
Prices: ["A","B","C"]
Queries: [5,5,10,10,20]
Expected: ["A","A","B","B","C"]
Actual: ["A","A","B","B","C"]

Test 7: PASS
Times: [100]
Prices: ["only"]
Queries: [99,100,1000000000]
Expected: ["","only","only"]
Actual: ["","only","only"]

Test 8: PASS
Times: [10,20,30,40,50]
Prices: ["A","B","C","D","E"]
Queries: [10,50]
Expected: ["A","E"]
Actual: ["A","E"]