FastPrepInvestable Periods 🐿️
Problems/Investable Periods 🐿️

Investable Periods 🐿️

MathWorks logoMathWorks● MediumFULLTIMEOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

A trading firm predicts the stock prices of a commodity for the next n days. A period of consecutive days is considered investable if the maximum price in the period is max_price, and the minimum price in the period is min_price. Find the number of investable periods in the next n days.

More formally, given an array price of length n, find the number of subarrays in which the maximum element is max_price and the minimum element is min_price. Note: A subarray is a sequence of consecutive elements of the array.

Function

countInvestablePeriods(price: int[], max_price: int, min_price: int) → long

Complete the function countInvestablePeriods in the editor below.

countInvestablePeriods has the following parameters:

  1. int price[n]: the predicted prices for the next n days
  2. int max_price: the maximum price of an investable period
  3. int min_price: the minimum price of an investable period

Returns long integer: the number of investable periods

Examples

Example 1

price = [4, 5, 3, 3, 1]max_price = 5min_price = 3return = 4

Here, the periods [4, 5, 3], [4, 5, 3, 3], [5, 3], and [5, 3, 3] are investable.

Example 2

price = [2, 2, 1, 5, 1]max_price = 2min_price = 1return = 2

The periods [2, 2, 1] and [2, 1] are investable.

Example 3

price = [1, 2, 3, 2]max_price = 3min_price = 2return = 3

The consecutive periods [2, 3], [2, 3, 2], and [3, 2] are investable as the min and max of these subarrays equal min_price and max_price respective. There are no otehr periods that satisfy this constraint. So, answer is 3.

Constraints

  • 1 ≤ n ≤ 10^5
  • 1 ≤ price[i] ≤ 10^9
  • 1 ≤ min_price ≤ max_price ≤ 10^9

More MathWorks interview questions

  • Earliest Time to Schedule a MeetingOA · Seen Nov 2026
  • Minimum Cost to Serve Requests on a RingOA · Seen Nov 2026
  • Minimum Tree Diameter After Removing K LeavesOA · Seen Nov 2026
  • Cities on Shortest Delivery RoutesOA · Seen Nov 2026
  • Repeated Mirror XOROA · Seen Nov 2026
  • Beautiful ArrangementONSITE INTERVIEW · Seen Jul 2026
  • Find Minimum Cost to Remove Array ElementsONSITE INTERVIEW · Seen Jul 2026
  • Group Shifted StringsONSITE INTERVIEW · Seen Jul 2026
  • Longest Valid ParenthesesONSITE INTERVIEW · Seen Jul 2026
  • Balancing TeamsOA · Seen Nov 2025

Similar questions at other companies

  • Earn the Most 🐝Capital One · OACapital One interview questions
  • Count Subarrays With Product Equal to KSalesforce · OASalesforce interview questions
  • Count Belt Sections With K Fruit Pairs (Forward Deployed Engineer)Boston Consulting Group · OABoston Consulting Group interview questions
  • Count Servers With No Requests in a Time WindowIBM · OAIBM interview questions
  • Reschedule Meetings for Maximum Free TimeFivetran · OAFivetran interview questions
  • Count Subarrays with at Least K Equal-Fruit PairsTikTok · OATikTok interview questions
  • Consistent LogsRippling · OARippling interview questions
  • Minimum Team Size From Every StartCognizant · OA
  • Minimum of Fixed-Window MaximaJuspay · OA
  • Sliding-Window Means with IEEE Special ValuesHeadlands Technologies · OAHeadlands Technologies interview questions

MathWorks interview experiences

  • MathWorks SWE Intern Phone Screens: 1 Experience
  • MathWorks Software Engineer in Test OA and HireVue: 1 Experience
All MathWorks interview questionsSee MathWorks hiring insights
Saved
class Solution {
    public long countInvestablePeriods(int[] price, int max_price, int min_price) {
      // write your code here
    }

}
Checking account…