[Fullstack] Interview Question must ask Nodejs, Javascript

Peter Chang
OrienteBar
Published in
6 min readJan 12, 2017

We are software engineer in javascript, below is collection of more than 20 questions that we asked or been asked in interview.

Content

  1. Design pattern
  2. Javascript
  3. Algorithm — question/answer
  4. Algorithm — knowledge
  5. Architect

Design pattern

Singleton

Singleton pattern restricts the instantiation of a class and ensures that only one instance of the class exists in lifecycle.

Factory

It comes under creational pattern as this pattern provides one of the best ways to create an object by calling a factory method.
In Factory pattern, we create object without exposing the creation logic to the client and refer to newly created object using a common interface.

Command

The Command pattern encapsulates actions as objects.
Good example

  • four terms always associated with the command pattern are command, receiver, invoker and client.

Javascript

1-

var foo = 2 + 10 + '20';   // foo = ?var qoo = ‘1’ + 2 + 10 + '20';   // qoo = ?var a = 0; 
var b = a;
a = 1; // b = ?
var a = {test: 0}; var b = a; a.test = 1; // b = ?var foo = {};
foo.bar = 'hello';
foo.length = !?

Algorithm

Question 1 :

Given a non-empty array of integers, return the k most frequent elements.

Given [1,1,1,2,2,3] and k = 2, return [1,2].

Answer:

- A simple solution is to use Hashing. Hash all words one by one in a hash table- O(1) — O(n)

Question 2 :

Given a value V, if we want to make change for V cents, and we have infinite supply of each of C = { C1, C2, .. , Cm} valued coins, what is the minimum number of coins to make the change?

Input: coins[] = {25, 10, 5}, V = 30
Output: Minimum 2 coins required
We can use one coin of 25 cents and one of 5 cents
Input: coins[] = {9, 6, 5, 1}, V = 11
Output: Minimum 2 coins required
We can use one coin of 6 cents and 1 coin of 5 cents

Answer:

a knapsack problem converts something that is NP-complete into something that is O(n^2)--you try all itemsC() --> count()
C({1,2,3}, 5)
/ \
/ \
C({1,2,3}, 2) C({1,2}, 5)
/ \ / \
/ \ / \
C({1,2,3}, -1) C({1,2}, 2) C({1,2}, 3) C({1}, 5)
/ \ / \ / \
/ \ / \ / \
C({1,2},0) C({1},2) C({1,2},1) C({1},3) C({1}, 4) C({}, 5)
/ \ / \ / \ / \
/ \ / \ / \ / \
. . . . . . C({1}, 3) C({}, 4)
/ \
/ \
. .

Question3 :

The Longest Increasing Subsequence:
For example, the length of the LIS for { 15, 27, 14, 38, 26, 55, 46, 65, 85 } is 6 and the longest increasing subsequence is {15, 27, 38, 55, 65, 85}.

Answer

- Binary Search Tree
- O(n logn)

Quick Sort

When you don’t need a stable sort and average case performance matters more than worst case performance. A quick sort is O(N log N) on average, O(N²) in the worst case.

Merge Sort

When you need a stable, O(N log N) sort, this is about your only option.

Hashing search

Terminology:

  • slot
  • bucket
  • overflow
  • collision
  • open addressing = closed hashing
  • probe
  • Clusters : cluster table, All rows with the same key value are stored together on disk.

Advantages:

  • Fast
  • accept disorder input
  • O(1), happens in the situation without collision overflow
  • Speed is independent to Size
  • Highly security, only the hash function is able to fetch data
  • easy to add / delete

Disadvantages:

  • wasted space(when overflow)
  • complex programming design
  • highly cast when collision happens

Complexity: Log2N

Architect

JavaScript: both for a frontend — React.js and backend — Node.js.

The paragraph below is quote from the site , Here is the architecture:

http://www.bebetterdeveloper.com/coding/architecture/serverless-system-architecture-using-aws.html

My Website — is a quite simple Single Page Application done in React using Flux pattern to handle UI changes.

  1. Website gets information about the products via a REST API implemented through AWS API Gateway, which calls AWS Lamdba function (written in Node.js) to actually get data from database.
  2. I use DynamoDB as a database, storing all of my products as JSON objects.
  3. UI is built using Bootstrap and some more advanced css styles to create flipping cards effects for products.

Watcher

Watcher is AWS Lambda function implemented in Node.js. It queries a database, gets all the products and runs scrapers for each product to retrieve a new price from online stores.

Scraper function is specific for each store, it loads shop’s html and gets/parses price fields using jQuery selectors. If there is change in price — product is updated with a new price and persisted to DynamoDB.

Watcher is scheduled to run every 4 hours using AWS CloudWatch Events.

Notifier

One more AWS Lambda function which listens to events in DynamoDB and if there is a price decrease — it sends email notification using AWS SNS service.

All AWS Lambda functions are written in JavaScript and tested using Mocha. Deployment is as easy as running “gulp deploy” task — it runs tests, gets all dependant node modules, zips all files together and uploads my JavaScript code to AWS as Lamdba functions.

Search system + DB + Node.js

In this article, we will discuss the technical architecture of an e-commerce system by using industry proven technologies like MongoDB, Node.js, and Elasticsearch.

These tools will help enable us to scale horizontally, utilize less system resources, and take advantage of non-blocking I/O to handle more traffic.

Here is the source.

Architecture Approach

Microservices: Choose a microservices architecture by considering the below points:

  • Easy to scale out horizontally based on the demand.
  • System resilience. We can achieve high availability.
  • There is a scope to extend/replace the functionality without a major impact on the entire system.

The above system is divided into the following services, based on the microservices architecture paradigm

  • Product Catalog Search Service
  • User Registration service
  • User Login Service
  • Shopping Cart Service
  • Product Detail Page Service
  • Checkout Service

Ecommerce Backend System

CSS

  1. Different inline-block, block
  2. 2 Ways of Vertical Alignment
  3. box-sizing: width+padding+border (box-sizing: border-box)
  4. background-size: cover/content

Browser

  1. Form Submit: Prefly-option
  2. Interaction without Javascript
  3. Header: Cross-origin / Http-only
  4. Rendering Process:
    JavaScript -> Style calculations -> Layout -> Painting -> Compositing

Reference:

https://stfalcon.com/en/portfolio/web-development/meinfernbus-de

https://dzone.com/articles/build-your-online-store-with-mongo-db-nodejs-and-e

http://www.bebetterdeveloper.com/coding/architecture/serverless-system-architecture-using-aws.html

https://sourcemaking.com/design_patterns/factory_method

--

--