LeetCode - 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.
Example:
MovingAverage m = new MovingAverage(3);
m.next(1) = 1
m.next(10) = (1 + 10) / 2
m.next(3) = (1 + 10 + 3) / 3
m.next(5) = (10 + 3 + 5) / 3

Java Solution:
class MovingAverage {

    /** Initialize your data structure here. */
    private Queue<Integer> q;
    private double sum;
    private int maxSize;
    public MovingAverage(Integer size) {
        q = new LinkedList<Integer>();
        maxSize = size;
        sum = 0;
    }
    
    public double next(int val) {
        if(q.size() != maxSize){
            q.offer(val);
            sum+=val;
            return sum / q.size();
        }else{
            sum-=q.peek(); // subtracting from sum before removing the first element
            q.poll();
            q.offer(val);
            sum+=val;
            return sum / q.size();
        }
    }
}

/**
 * Your MovingAverage object will be instantiated and called as such:
 * MovingAverage obj = new MovingAverage(size);
 * double param_1 = obj.next(val);
 */

Java - Concurrency / Threads


What is a Process in Concurrency / Threads ?

A process is a Unit of Execution that has it's own memory space.

When ever we run an application , we are starting a new process . 

Example : Running a Java FX application.
                 Running any Java console applications .  Nothing but we are starting a new process .

Each instance  of Java Virtual machine runs as a process (For most of them).


When ever two applications or process's are running , they have their own memory space or heap .

Applications or process's never share the memory / heap between them.

Thread :

What is a Thread ?

Thread is a Unit of Execution with in a Process . 

A process can have multiple threads .  Default in Java we have at least one thread called main thread.

For UI we have JavaFX application thread.

Our code runs in main thread or JavaFX app thread (UI) or threads that are explicitly provided by us.

Every Java application / process has multiple threads like memory management , Input / Output operations (I/O operations) .  Which we don't create , provided by java itself.

Unlike process's , threads can share files and memory of it's process / application.

In addition to process's memory thread has it's own memory called thread stack which can't be shared to other thread. Only that particular thread can access it's thread stack.


                                           Application runs as a  Process -> Heap Memory

                                                            Thread -> Thread Stack

                                                                          ->  Thread 1
                                                           Process   ->  Thread 2
                                                                           ->   Thread 3


Can't we run our entire application in one single thread as how we are running our application as a single process ?

Consider a scenario that you a fetching some data from an application . Where the application has several databases and also some information that it fetches from internet .

If we execute entire data fetching from all databases and internet in one main thread it will take more time or more latency because it act's in a linear fashion . User's of application may think app is down or got stuck by the time response is provided by main thread.

Using multiple threads , each thread will work on different databases and one thread will fetch data from internet and in the mean while main thread can provide status updates to user.

Finally all information can be collated and end result can be  displayed to User.

Example : you are watching a movie and parallelly downloading a file . Both are two tasks / threads running in parallel.


Java provides thread related classes to achieve concurrency .

Threads execution will depend on JVM and operating system on which they are running.











Java 8 Tutorial and Examples

Java 8 Introduction

Java 8 is the major feature release of  Java Programming language .

Java 8 released in  March 18 , 2014.

Reference :

https://en.wikipedia.org/wiki/Java_version_history

With Java 8 Release . Java provided support for various new features as below.


  • Functional Programming
  • New API's for date time manipulation
  • New Java script engine
  • Streaming API's

Features of Java 8 :


Lambda Expression :

Added Functional processing capability to java. Which means treating functionality as a method argument or treating code as data.

Method References:

Treating functions as parameters .  Referencing functions with their names instead of  invoking or calling them directly.

Nashorn , Javascript Engine :

New java script engine to execute java Script Code.

Date Time API:


New set of packages added that provide additional features and functions.


Stream API:

The Stream API is integrated into the Collections API.

Stream API helps for bulk operations on collections . Like sequential or parallel map-reduce transformations.

Default Method:

Interfaces can have default methods . Default methods can be implemented.

Optional :

This feature is to handle null value properties.

Java FX , Security , Collections many other updates and improvements has been done for new version of Java 8 .

Reference :














Sorting - Introduction


In general in Computers more than 25% of run time is taken for sorting .

Sometimes  the Computing time for sorting is more than 50% for some installations.


- Above details mentioned by Donald  Kuth in The Art Of Computer Programming Vol 3







Top Ten Programming Languages In 2020

1. Python
2. Java
3. JavaScript
4. C and C++
5. Golang

6. R
7. Kotlin
8. C#

 9. Swift

10. PHP

Top 10 most Popular skills of 2020

1.  Python 

2. React (Web)

3. Angular


< 4. Machine Learning

5. Docker


< 6.Django

7. CompTia

8.Amazon AWS

9.Deep Learning


< 10.React Native(mobile)

Featured Post

H1B Visa Stamping at US Consulate

  H1B Visa Stamping at US Consulate If you are outside of the US, you need to apply for US Visa at a US Consulate or a US Embassy and get H1...