{"id":"209be658-2292-4b86-a222-7eda9b32f9f0","name":"Moving Average From Data Stream","description":"Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window.\r\n\r\nImplement the MovingAverage class:\r\n1. MovingAverage(int size) Initializes the object with the size of the window size.\r\n2. double next(int val) Returns the moving average of the last size values of the stream.","inputFormat":"Input is managed for you\r\n","outputFormat":"Output is managed for you","constraints":"1. 1 <= size <= 1000\r\n2. -10^5 <= val <= 10^5","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static class MovingAverage {\r\n public MovingAverage(int size) {\r\n\r\n }\r\n\r\n public double next(int val) {\r\n\r\n }\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader read = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n int size = Integer.parseInt(read.readLine());\r\n MovingAverage obj = new MovingAverage(size);\r\n PrintWriter out = new PrintWriter(System.out);\r\n while (read.ready()) {\r\n int val = Integer.parseInt(read.readLine());\r\n double avg = obj.next(val);\r\n\r\n StringBuilder ans = new StringBuilder(String.format(\"%.5f\", avg));\r\n while (ans.charAt(ans.length() - 2) != ''.'' && ans.charAt(ans.length() - 1) == ''0'') {\r\n ans.deleteCharAt(ans.length() - 1);\r\n }\r\n out.println(ans);\r\n }\r\n out.close();\r\n }\r\n}\r\n"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"3\r\n1\r\n10\r\n3\r\n5","sampleOutput":"1.0\r\n5.5\r\n4.66667\r\n6.0\r\n","questionVideo":"https://www.youtube.com/embed/nJ-8tw41NwM","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"8c6022a5-8654-4226-918f-8110af738bd4","name":"Stacks For Intermediate","slug":"stacks-for-intermediate-688","type":0},{"id":"47f6617b-7146-4629-be58-d0d1652874f7","name":"Moving Average From Data Stream","slug":"moving-average-from-data-stream","type":1}],"next":{"id":"d8cff620-12e2-4ca7-a871-5fe440065fac","name":"Moving Average From Data Stream MCQ","type":0,"slug":"moving-average-from-data-stream-mcq"},"prev":{"id":"5b195b3a-3b90-4c93-941a-8670eff06cf2","name":"Number Of Recent Calls","type":3,"slug":"number-of-recent-calls"}}}

Moving Average From Data Stream

Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window. Implement the MovingAverage class: 1. MovingAverage(int size) Initializes the object with the size of the window size. 2. double next(int val) Returns the moving average of the last size values of the stream.

{"id":"209be658-2292-4b86-a222-7eda9b32f9f0","name":"Moving Average From Data Stream","description":"Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window.\r\n\r\nImplement the MovingAverage class:\r\n1. MovingAverage(int size) Initializes the object with the size of the window size.\r\n2. double next(int val) Returns the moving average of the last size values of the stream.","inputFormat":"Input is managed for you\r\n","outputFormat":"Output is managed for you","constraints":"1. 1 <= size <= 1000\r\n2. -10^5 <= val <= 10^5","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static class MovingAverage {\r\n public MovingAverage(int size) {\r\n\r\n }\r\n\r\n public double next(int val) {\r\n\r\n }\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader read = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n int size = Integer.parseInt(read.readLine());\r\n MovingAverage obj = new MovingAverage(size);\r\n PrintWriter out = new PrintWriter(System.out);\r\n while (read.ready()) {\r\n int val = Integer.parseInt(read.readLine());\r\n double avg = obj.next(val);\r\n\r\n StringBuilder ans = new StringBuilder(String.format(\"%.5f\", avg));\r\n while (ans.charAt(ans.length() - 2) != ''.'' && ans.charAt(ans.length() - 1) == ''0'') {\r\n ans.deleteCharAt(ans.length() - 1);\r\n }\r\n out.println(ans);\r\n }\r\n out.close();\r\n }\r\n}\r\n"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"3\r\n1\r\n10\r\n3\r\n5","sampleOutput":"1.0\r\n5.5\r\n4.66667\r\n6.0\r\n","questionVideo":"https://www.youtube.com/embed/nJ-8tw41NwM","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"8c6022a5-8654-4226-918f-8110af738bd4","name":"Stacks For Intermediate","slug":"stacks-for-intermediate-688","type":0},{"id":"47f6617b-7146-4629-be58-d0d1652874f7","name":"Moving Average From Data Stream","slug":"moving-average-from-data-stream","type":1}],"next":{"id":"d8cff620-12e2-4ca7-a871-5fe440065fac","name":"Moving Average From Data Stream MCQ","type":0,"slug":"moving-average-from-data-stream-mcq"},"prev":{"id":"5b195b3a-3b90-4c93-941a-8670eff06cf2","name":"Number Of Recent Calls","type":3,"slug":"number-of-recent-calls"}}}
plane

Editor


Loading...

Moving Average From Data Stream

easy

Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window. Implement the MovingAverage class: 1. MovingAverage(int size) Initializes the object with the size of the window size. 2. double next(int val) Returns the moving average of the last size values of the stream.

Constraints

1. 1 <= size <= 1000 2. -10^5 <= val <= 10^5

Format

Input

Input is managed for you

Output

Output is managed for you

Example

Sample Input

3 1 10 3 5

Sample Output

1.0 5.5 4.66667 6.0

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode