## Posts

### Data structure problem 3: Implement a Stack With Minimum

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. Implement the MinStack class: MinStack() initializes the stack object. void push(int val) pushes the element val onto the stack. void pop() removes the element on the top of the stack. int top() gets the top element of the stack. int getMin() retrieves the minimum element in the stack. Input ["MinStack","push","push","push","getMin","pop","top","getMin"] [[],[-2],[0],[-3],[],[],[],[]] Output [null,null,null,null,-3,null,0,-2] Explanation MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0); minStack.push(-3); minStack.getMin(); // return -3 minStack.pop(); minStack.top();    // return 0 minStack.getMin(); // return -2 Solution: var  MinStack =  function ( )  { this .elements = []; }; /** @param {number} x @return {void} */ MinStack.prototype.push = function ( x ) { this .element

### Projection in MongoDB

In MongoDB, projection means selecting only the necessary data rather than  selecting the whole of the data of a document. If a document has 5 fields and  you need to show only 3, then select only 3 fields from them. You may also like MongoDb insert. 1 )Projection in find . Here 0 means not to project i . e exclusion and 1 means project i . e inclusion as per MongoDB . Projection cannot have a mix of inclusion and exclusion but only the _id field can exclude with inclusion . db . collection( "companies" ) . find({name: "Dvara SmartGold" },{projection: { _id: 0 , id : 1 , name: 1 } }) . toArray() Or db . collection( "companies" ) . find({name: "Dvara SmartGold" }) . project({_id: 0 , id : 1 , name: 1 }) . toArray() This will give us all documents inside companies collection as per filter with only name and id fields . 2 )Projection in findOne . db . collection( "companies" ) . findOne({name: &qu

### Javascript event-loop and Nodejs event-loop.

The Event Loop in the Browser or Node is not part of V8. The Event Loop Is Part of a different application/dependency/library which is provided by the Browser or Node. They do not use V8's event loop. V8 does implement an event loop, it is there . However, it is meant to be overridden or replaced, which is something both Chrome and NodeJS happen to do. The Nodejs event loop implementation differs from the browser-based event loop one. While Nodejs uses the Google V8 as its runtime, it does not use V8 to implement the event loop. Nodejs uses the Libuv library (written in C) to implement the event loop.  Browser (Chrome):  V8 just executes your JavaScript (If and else statements, for statements, functions, arithmetic operations e.t.c) and then hands over operations to Libevent . In the Browser(e.g Chrome) apart from JavaScript Engine V8 (Chrome uses V8), the browser also contains different applications/dependencies/libraries which can do a variety of things like sending HTTP requests