TCS NQT

  1. Suppose, for a stock, we have a series of N daily price quotes. For each day, the task is to find how many such continuous days (including current day) are there prior to the current day where the price of the stock was less than or equal to the price on the current day.
    Hint: Start counting continuously from the current day to all days before it; stop when the stock is higher than the current day.
    For example, 6 day’s stock prices are as follows: {100, 60, 70, 65, 80, 85}

    Stock span chart:
    Output 1 1 2 1 4 5
    Stock Price 100 60 70 65 80 85
    Day 1 2 3 4 5 6

    For day1: count is always 1, hence the first output = 1.
    Day2: Price is 60, and there is no day before it where the price was less than 60, hence the second output = 1.
    Day3: Price is 70, which is greater than the price on day2 but less than price on day1, hence the third output = 2.
    Day4: Price is 65, which is less than the price on day3, hence the fourth output = 1.
    Day5: Price is 80, which is greater than the price on day5, day4, day3 and day2, hence the fifth output = 4.
    Day6: Price is 85, which is greater than the price on day5, day4, day3 and day2, hence the sixth output = 5
    As per the explanation, the output is {1, 1, 2, 1, 4, 5}.

    Sample Input

    6
    100
    60
    70
    65
    80
    85

    Sample Output

    1 1 2 1 4 5

    Constraints

    2 <= N <= 100
    0 <= L[i] <= 100000

    Input Format

    First input, the candidate has to write the code to accept a single integer value N denoting the number of days.
    Second input, the code should accept N number of integer values separated by a new line.

    Output Format

    Output should be N integer values from the list separated by a single space character.

    Click Here for Solution



No comments:

Post a Comment

Total Pageviews