Feature #2: Settling Period

Implementing the "Settling Period" feature for our "Stocks" project.


The settling period is the time that must elapse before money changes hands after a stock trade is completed. We don’t want to allow another trade in the same stock before the expiry of the settling period. So, a person can’t trade more than one stock of the same company at once, and the user must wait for the settling period before another stock of the same company can be traded.

We are given a list of letters, where each letter represents the stock of a company for which a stock trade must be made. The stocks will be mapped to letters to form this input array. Letters can repeat to represent that multiple trades need to be made for the same company’s stocks. We want to make all the given trades as quickly as possible. The constraint is that two trades of the same company must be separated by at least k intervening periods; these could be trades of other companies or idle periods. The parameter k defines the settling period. We need to calculate the minimum amount of time required to trade all the stocks.

For example, if a user wants to trade four stocks of APPLE, two stocks of TESLA and one stock of MICROSOFT in the same transaction, an input array like ['A', 'A', 'A', 'T', 'T', 'M', 'A'] would be given.

This is an example of mapping of stocks to characters’ array:

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.