Twitter | Phone Screen | RGB Stream
Anonymous User
4269

You have a set of users visiting your website. Each user picks a color represented by RGB. R, G, B can take values between 0 and 255.

You have a stream of users coming and at any point you have to calculate the mean and median for RGB colors seen so far.

Example:

setRGB(), red, green, blue, {0, ..., 255}
showMean()
showMedian()
RGB                 Mean                Median
(0, 0, 0),        (0, 0, 0)              (0, 0, 0)
(34, 232, 9), (17, 116, 4.5)   (17, 116, 4.5)
(43, 21, 34), (77/3, .., ..)        (34, 21, 9)

Answer : since the values for each color take 0-255 values, we can have 3 arrays or a 3*255 array, each of size 255. Each index will hold the number of times a value has occurred.
Mean --> mean = (sum(array[i]*i for i in array))/self.length_of_input
For median, we need to find the median position and handle odd/even len of input

Comments (2)