Doubt in interview question

In the given question we have to obtain a running stream of numbers and then calculate the variance and mean at any point of time. However I have implemented it using the random module function randint. Please guide how to generate a random stream of numbers and also how to interrupt the process at a given point of time. Also is there a way in which we can keep a count of the number of elements in the array. Here I am specifying the value of N manually. Below is a snapshot of the implementation I have done:

Hello @Charu510, yes we can use the while loop with always true condition.

while True:
    pass

Here you can write your code and it will be generating the stream of random nos. what you need to do inside this loop is, then input some no. of your choice and also increment the count of the total nos. by 1. You need to declare a variable total no. outside the loop to keep the track of nos. And you can provide a certain condition such as when you will enter -1 then the loop will break like,

n = int(input())
if n == -1:
    break

I hope it is somewhat clear to you and feel free to ask if there is still any confusion.
Thanks :slight_smile:

I tried by the method and the input stream stops as and when i enter -1 but I am not getting the correct count of the random numbers stream. This is how I implemented:

Please guide

Why are you taking 10 nos. all together. Take them one by one and then increment the count by 1. What are you doing is taking 10 nos. and incrementing the total count by 1 which is not right.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.