Random Interview Questions

A recount of my interview questions…

When will you use NoSQL and SQL?

  • SQL is designed for vertical scaling where we increase the computation power of a single server machine. This means that we are limited in this aspect. If we want to scale horizontally, then it involves much more effort in partitioning, sharding, clustering etc. SQL joins have bad time complexity.
  • On the other hand, with NoSQL, we can simply add one more node as per needed.

Acid Compliance:

  • SQL ensures ACID compliance.
  • NoSQL may compromise on ACID. For eg, lazy writes… follows the BASE consistency model, which means: Basic Availability: This means that while the database guarantees the availability of the data, the database may fail to obtain the requested data or the data may be in a changing or inconsistent state. Soft state: The state of the database can be changing over time. Eventual consistency: The database will eventually become consistent, and data will propagate everywhere at some point in the future.

Complexity of Queries:

  • SQL supports relations between data types. It efficiently executes queries and retrieves and edits data quickly.
  • A NoSQL database provides a ton of flexibility in the types of data that you can store, but because of the potentially large differences in data structures, querying isn’t as efficient as with a SQL database. You will have to perform extra processing on the data.

Data Schema:

  • NoSQL allows for fast change to the database schema. The data you store in a NoSQL database does not need a predefined schema like you do for a SQL database. Rather, the data can be column stores, document-oriented, graph-based, or key-value pairs. This provides much more flexibility and less upfront planning when managing your database.
  • SQL requires well defined schemas.

Difference between B+ Tree and BST

  • B+ Tree can have more than 1 key values and hence more than 1 children. The time to search for a value takes more time than BST.

Difference between B- Tree and B+ Tree

  1. B+ trees don’t store data pointer in interior nodes, they are ONLY stored in leaf nodes. This is not optional as in B-Tree. This means that interior nodes can fit more keys on block of memory. Because B+ trees don’t have data associated with interior nodes, more keys can fit on a page of memory. Therefore, it will require fewer cache misses in order to access data that is on a leaf node.
  2. The leaf nodes of B+ trees are linked, so doing a linear scan of all keys will requires just one pass through all the leaf nodes. A B tree, on the other hand, would require a traversal of every level in the tree. This property can be utilized for efficient search as well since data is stored only in leafs. The leaf nodes of B+ trees are linked, so doing a full scan of all objects in a tree requires just one linear pass through all the leaf nodes. A B tree, on the other hand, would require a traversal of every level in the tree. This full-tree traversal will likely involve more cache misses than the linear traversal of B+ leaves.
  3. In both B+ and B- Tree, the leaf nodes has to be at the same level. But BST is not.

B+ trees use some clever balancing techniques to make sure that all of the leaves are always on the same level of the tree, that each node is always at least half full (rounded) of keys, and (therefore) that the height of the tree is always at most ceiling(log(k)) of base ceiling(n/2) where k is the number of values in the tree and n is the maximum number of pointers (= maximum number of nodes + 1) in each block. This means that only a small number of pointer traversals is necessary to search for a value if the number of keys in a node is large.

  • The root node in a B+-Tree can have a minimum of one key and two pointers.

What happens when the Hash table is full? Why do you want to double the size? Why not +100?

(From a stack overflow answer)

Hash-tables could not claim “amortized constant time insertion” if, for instance, the resizing was by a constant increment. In that case the cost of resizing (which grows with the size of the hash-table) would make the cost of one insertion linear in the total number of elements to insert. Because resizing becomes more and more expensive with the size of the table, it has to happen “less and less often” to keep the amortized cost of insertion constant.

Most implementations allow the average bucket occupation to grow to until a bound fixed in advance before resizing (anywhere between 0.5 and 3, which are all acceptable values). With this convention, just after resizing the average bucket occupation becomes half that bound. Resizing by doubling keeps the average bucket occupation in a band of width *2.

What makes a good hash function?

  1. The hash function uses all the input data. If the hash function doesn’t use all the input data, then slight variations to the input data would cause an inappropriate number of similar hash values resulting in too many collisions.
  2. The hash function “uniformly” distributes the data across the entire set of possible hash values. If the hash function does not uniformly distribute the data across the entire set of possible hash values, a large number of collisions will result, cutting down on the efficiency of the hash table. -> Clustering problem
  3. The hash function generates very different hash values for similar strings. In the real world, many data sets contain very similar data elements. We would like these data elements to still be distributable over a hash table.

How do JavaScript work? Are JavaScript Single Threaded?

  • V8 -> Java script run time. The environment to run JavaScript.
  • What happens when things are slow? We have to wait for them one by one until they are done?
  • This is a problem. If we have this in a browser, then the browser appears “frozen” and cannot do anything.
  • The solution: Asynchronous Callbacks
  • How the stack looks like for above is that we have console.log(“Hi”) followed by console.log(“JSConfEU”). Then after a while, console.log(“There”) appears in the stack.
  • The runtime can only run code line by line and must finish executing before going to the next. So how does the above happen? How can we skip the function in the middle? The reason is that a browser is made of more than just the run time.

The web API when it is done, pushes the callback to the task queue.

The event loop will be in charge of looking at the stack and the task queue. If the stack is empty, it pushes the first thing in the task queue onto the stack.

The call back gets executed.

What is NodeJS?

How does multi-threading work?

API system rate limiting

  • Rate Limiting is important where you want to ensure a good quality of service for every consumer.
  • There are two types of rate limiting: Backend Rate Limiting & Application Rate Limiting.

Backend Rate Limiting:

  • API Owners typically measure processing limits in Transaction Per Second (TPS) and impose a limit on it.

Application Rate Limiting:

  • API Owners enforce a limit on the number of requests a client can consume.
Can check from HTTPS Headers to find out about rate limit

Limiting calls made to Third Party APIs:

  • For example if your client call the Google Map API directly, there is not much you can do to limit it. If the rate limited API is accessed via some form of backend process, it is then easier to do rate limiting.

Implementation

(1) Request Queues: Sets the rate limit at two requests per second and places the rest in a request queue.

(2) Trottling: Set up a temporary state and allow the API to assess each request. When the throttle is triggered, a user may either be disconnected or simply have their bandwidth reduced.

(3) Algorithms

Find out more here: https://nordicapis.com/everything-you-need-to-know-about-api-rate-limiting/

The small number of data points needed to assess each request makes the sliding window algorithm an ideal choice for processing large amounts of requests while still being light and fast to run.

What is dependency injection and how does it affect maintainability?

In the code, the interaction between objects should be as minimal as possible. You should ask for precisely what you need and nothing more. If you need something, you should ask for it — have it “delivered” to you instead of trying to create it yourself.

This allows for having loosely coupled code.

If your classes are loosely coupled and follow the single responsibility principle — the natural result of using DI — then your code will be easier to maintain.

Simple, stand-alone classes are easier to fix than complicated, tightly coupled classes.

Thread Pooling

What is it?

Creating a new thread comes with a performance overhead compared to reusing a thread that is already created. That is why reusing an existing thread to execute a task can result in a higher total throughput than creating a new thread per task.

Additionally, using a thread pool can make it easier to control how many threads are active at a time. Each thread consumes a certain amount of computer resources, such as memory (RAM), so if you have too many threads active at the same time, the total amount of resources (e.g. RAM) that is consumed may cause the computer to slow down.

How a Thread Pool Works?

Instead of starting a new thread for every task to execute concurrently, the task can be passed to a thread pool. As soon as the pool has any idle threads the task is assigned to one of them and executed. Internally the tasks are inserted into a Blocking Queue which the threads in the pool are dequeuing from. When a new task is inserted into the queue one of the idle threads will dequeue it successfully and execute it. The rest of the idle threads in the pool will be blocked waiting to dequeue tasks.

http://tutorials.jenkov.com/java-concurrency/thread-pools.html

RESTful Web service

REST is an architecture style for designing network applications. It relies on a stateless, client-server protocol (HTTP).

What does REST stand for?

“Primary characteristics of REST are being stateless and using GET to access resources. In a truly RESTful application, the server can restart between calls as data passes through it.”

“REST has many advantages. It’s easy to scale, flexible and portable and works independently from the client and server, which makes development less complex. ”

Independent of the programming platform.

Name some of the commonly used HTTP methods used in REST based architecture?

200 OK

404 NOT FOUND

201 CREATED

405 METHOD NOT ALLOWED

Post

When creating a new resource, POST to the parent and the service takes care of associating the new resource with the parent, assigning an ID (new resource URI), etc.

POST is neither safe nor idempotent. It is therefore recommended for non-idempotent resource requests. Making two identical POST requests will most-likely result in two resources containing the same information. On successful creation, return HTTP status 201, returning a Location header with a link to the newly-created resource with the 201 HTTP status.

Get

The HTTP GET method is used to **read** (or retrieve) a representation of a resource. In the “happy” (or non-error) path, GET returns a representation in XML or JSON and an HTTP response code of 200 (OK). In an error case, it most often returns a 404 (NOT FOUND) or 400 (BAD REQUEST).

They are considered safe. That is, they can be called without risk of data modification or corruption

Put

PUT-ing to a known resource URI with the request body containing the newly-updated representation of the original resource.

However, PUT can also be used to create a resource in the case where the resource ID is chosen by the client instead of by the server. In other words, if the PUT is to a URI that contains the value of a non-existent resource ID.

PUT is not a safe operation, in that it modifies (or creates) state on the server, but it is idempotent. In other words, if you create or update a resource using PUT and then make that same call again, the resource is still there and still has the same state as it did with the first call.

Delete

On successful deletion, return HTTP status 200 (OK) along with a response body, perhaps the representation of the deleted item (often demands too much bandwidth), or a wrapped response (see Return Values below). Either that or return HTTP status 204 (NO CONTENT) with no response body. In other words, a 204 status with no body, or the JSEND-style response and HTTP status 200 are the recommended responses.

Is caching present?

Usually, browsers treat all GET requests cacheable. POST requests are not cacheable by default but can be made cacheable if either an Expires header or a Cache-Control header with a directive, to explicitly allows caching, is added to the response. Responses to PUT and DELETE requests are not cacheable at all.

The Expires HTTP header specifies an absolute expiry time for a cached representation.

Cache-control determine whether a response is cacheable, and if so, by whom, and for how long e.g. max-age or s-maxage directives.

Difference between PUT and PATCH?

Unlike PUT, PATCH applies a partial update to the resource.

This means that you are only required to send the data that you want to update, and it won’t affect or change anything else. So if you want to update the first name on a database, you will only be required to send the first parameter; the first name.

For example, when you want to change the first name of a person in a database, you need to send the entire resource when making a PUT request.

What is a resource in REST?

What is the disadvantages of REST?

For example, a request like, “Return all updated records from the last 3 hours containing the word cat” does not lend itself to being expressed as a path, so is likely to have to be implemented with some combination of URI path, query parameters, and perhaps request body.

(2) RESTful APIs typically rely on a few http methods (GET, POST, PUT, DELETE, perhaps PATCH), but many common client/server operations can only awkwardly be shoehorned into the standard methods. “Move expired documents to the archive folder” is an example of an application-specific custom verb that’s outside of typical http methods and CRUD operations.

https://www.quora.com/What-are-the-drawbacks-of-using-RESTful-APIs

Is HTTP same as REST?

Representational State Transfer, or REST, is a set of constraints that ensure a scalable, fault-tolerant and easily extendible system. The world-wide-web is an example of such system (and the biggest example, one might say). REST by itself is not a new invention, but it’s the documentation on such systems like the world-wide-web.

One thing that confuses people, is that REST and HTTP seem to be hand-in-hand. After all, the world-wide-web itself runs on HTTP, and it makes sense, a RESTful API does the same. However, there is nothing in the REST constraints that makes the usage of HTTP as a transfer protocol mandatory. It’s perfectly possible to use other transfer protocols like SNMP, SMTP and others to use, and your API could still very well be a RESTful API.

In practice, most — if not all — RESTful APIs currently use HTTP as a transport layer, since the infrastructure, servers and client libraries for HTTP are widely available already

Note that there is also a big difference between a RESTful API and a HTTP API. A RESTful API adheres ALL the REST constraints set out in its “format” documentation (in the dissertation of Roy Fielding). A HTTP API is ANY API that makes use of HTTP as their transfer protocol. This means that even SOAP can be considered a HTTP API, as long as it will use HTTP for transport, but most HTTP APIs will make more and better use of the infrastructure and possibilities of HTTP. Most HTTP APIs can be very close to becoming a truly RESTful API. This can be defined by their Richardsons maturity level.

How does a browser loads a website?

  1. Process HTML markup and build the DOM tree.
  2. Process CSS markup and build the CSSOM tree.
  3. Combine the DOM and CSSOM into a render tree.
  4. Run the layout on the render tree to compute the geometry of each node.
  5. Paint the individual nodes to the screen.

There are a few things we can do to improve the time it takes to render a web page. Fewer embedded file requests, smaller files being requested, and reducing the number of render blocking resources will all improve performance, but they aren’t the only things.

What is the purpose of -> in Java?

Lambda Expression

Anonymous Function

Normal function definition:

function hello() {
alert('Hello world');
}
hello();

Anonymous function definition:

var anon = function() {
alert('I am anonymous');
}
anon();

So, what is GitHub Flow?

How to improve frontend loading?

  1. Minification
  2. Pre fetch
  3. Remove unnecessary images, JS
  4. CDN and Caching

Minification

The End :)

This is a repository of my thoughts on my personal life, my random interests & notes taken down as I navigate my way through the tech world!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store