• +91 9723535972
  • info@interviewmaterial.com

RxJS Interview Questions and Answers

RxJS Interview Questions and Answers

Question - 21 : - What is the difference between Cold and Hot Observables in RxJS?

Answer - 21 : -

In simple words, the concept of cold and hot Observable can be defined as the following:

When the data is produced by the Observable itself, t is called the cold Observable. When the data is produced outside the Observable, it is called hot Observable.

Let's see the differences between Cold Observables and Hot Observables:

Cold Observables

Hot Observables

We can call an Observable "cold" when the data is produced inside the Observable.

We call the Observable "hot" when the data is produced outside the Observable.

Cold observables start to run upon subscription.

Hot observables produce values even before a subscription is made.

The Cold observable sequence only starts pushing values to observers when subscribe is called.

Hot observables such as mouse move events, stock pickers or WebSocket connections are already produced in values even before the subscription is active.

The cold Observable starts running upon subscription.

The hot Observable produces values before subscriptions.

The cold Observable sequence starts pushing values.

In cold Observable, the data producer is outside the Observable.

In cold Observable, the data is produced inside the Observable so, we cannot share the data between multiple subscribers. Two Observables that subscribe at more or less the same may receive two different values. We call this behavior "unicasting."

As we know that the data is produced outside the Observable in hot Observable, so it can share data between multiple subscribers in hot Observable. This behavior is "multicasting."

Question - 22 : - What do you understand by the Actor Model in RxJS?

Answer - 22 : -

An actor model can do the following things:

  • An Actor model specifies that your concurrency primitives are actors.
  • It can send messages to any actors they know about.
  • It can receive a message and decide what to do next depending on the content of the message.
  • It can create new actors and provides certain guarantees, such as any actor will only handle a single message at a time and messages sent by actor X to actor Y will arrive in the order they were sent.

Question - 23 : -
What does a subject do in RxJS?

Answer - 23 : -

RxJS subject is a special type of observable that allows values to be multicast to many observers. RxJS subjects are multicast instead of plain observables, which are unicast. The subject is the equivalent of an event emitter and the only way of multicast in a value or event to multiple observers. Subject implements both observable and observer interfaces. Every subject is observable so that you can subscribe to it. Every subject is an observer. It means that you have next, error, and complete methods, so you can send values, error subject or completed.

Types of Subjects

  • Subject
  • ReplaySubject
  • BehaviorSubject
  • AsyncSubject

Question - 24 : - What are the differences between Subject, BehaviorSubject and ReplaySubject in RxJS?

Answer - 24 : -

Subject
In the RxJS Subject, Observers who are subscribed later do not obtain the data values emitted before their subscription.

ReplaySubject
In RxJS ReplaySubject, Observers who are subscribed at a later point receives data values issued before their subscription. It operates by using a buffer that holds the values emitted and re-emits them once new Observers are subscribed.

BehaviorSubject
BehaviorSubject functions similar to ReplaySubject but only re-issues the last emitted values. So, it should be used when you are interested in the observer's last/current value.

Question - 25 : - What is RxJS Map, and what do you understand by Higher-Order Observable Mapping?

Answer - 25 : -

RxJS map operator facilitates us to project the payload of the Observable into something else. We can see the powerful features of Observables when we start using Rx operators to transform, combine, manipulate, and work with sequences of items emitted by Observables.

RxJS Higher-Order Observable Mapping
We map source observable emitted value into other Observable in higher-order mapping instead of mapping a flat value like 1 to another value like 10.! The result is an Observable higher order.

Question - 26 : - When should we use the switchMap, mergeMap and concatMap in RxJS?

Answer - 26 : -

There are mainly four types of mapping operators used in RxJS: concatMap(), mergeMap(), switchMap() and exhaustMap(). All of these operators are mapping or flattening operators used to flatten observables, but they are applicable in very different scenarios. The switchMap and mergeMap are the most powerful and frequently used operators. Let's see when we use these operators:
concatMap() Operators
Following is the sample code of concatMap() Operators:

this.form.valueChanges  
.pipe(  
    concatMap(formValue => this.http.put("/api/book/",formValue))  
)  
.subscribe(  
    response =>  ... handle successful ...,  
    err => ... handle error ...        
);  
The two main benefits of using concatMap() operator are that we no longer have to use nested subscribes with higher-order mapping operator, and the second is, all http requests are sent to the backend sequentially.

This is how the concatMap operator ensures that the requests still occur in sequence:

  • concatMap takes each form value and transforms it into an observer HTTP, known as an inner observer.
  • concatMap subscribes to the inner Observable and sends its output to the Observable result
  • The second form of value can come more quickly than is needed to request in the backend the previous form value. When this occurs, the new form value is not converted to an HTTP request immediately.
mergeMap() Operator
Unlike the RxJS concatMap operator, mergeMap() will not wait until the Observable finishes until the next Observable is subscribed.

This is how the mergeMap operator works:

  • In mergeMap operator, every Observable source value is mapped in an internal Observable.
  • The inner Observable is then subscribed by mergeMap.
  • When the inner observables emit new values, the output Observable immediately reflects them.
  • In the mergeMap, unlike the concatMap operator, we do not need to wait until the previous inner observable is completed.
switchMap() Operator
Unlike the mergeMap operator, in the switchMap operator, we unsubscribe the previous Observable before subscribing to the new Observable if the new Observable begins to emit the values.

Question - 27 : - What is Back-Pressure in Reactive Programming?

Answer - 27 : -

According to the Wikipedia definition, Back-Pressure is resistance or force opposing the desired flow of fluid through pipes. But this definition belongs to fluid dynamics. In the context of software, the definition will be changed to flow of data within software instead of fluid through pipes. So, the definition would be-

Back-Pressure is a resistance or force opposing the desired flow of data through software.
When one component is struggling to keep up, the entire system needs to respond sensibly. It is unacceptable for the component under stress to fail or to drop messages in an uncontrolled fashion. Since it is not easy to handle and can't fail, it should communicate that it is under stress to upstream components and get them to reduce the load. This back-pressure is an important feedback mechanism that facilitates systems to respond to load rather than collapse under such a situation gracefully. The back-pressure may cascade up to the user, at which point responsiveness may degrade. Still, this mechanism will ensure that the system is resilient under load and will provide information that may allow the system to apply other resources to make easy the load by distributing it.

In simple words, we can say that Back-pressure provides the strategies for coping with Observables that produce items more rapidly than their observers consume them.

Question - 28 : - What do you understand by Elasticity in contrast to Scalability?

Answer - 28 : -

In the IT infrastructure, the term "Elasticity" can be defined as the ability to quickly expand or cut back capacity and services without obstructing the infrastructure's stability, performance, security, governance or compliance protocols.

It means that the throughput of a system scales up or down automatically to meet varying demand as a resource is proportionally added or removed. The system needs to be scalable to allow it to benefit from the dynamic addition or removal of resources at runtime. Elasticity, therefore, builds upon Scalability and expands on it by adding the notion of automatic resource management.

Question - 29 : - What is the difference between Failure and Error?

Answer - 29 : -

Failure

Error

A failure can be defined as an unexpected event within a service that prevents it from functioning normally. When a failure occurs, it can generally prevent responses to the current and possibly all following client requests.

Errors are different from failures. An error is a common condition that can appear during input validation that will be communicated to the client as part of the message's normal processing.

Failures are unexpected, and they require intervention before the system can resume at the same level of operation as earlier.

Errors are an expected part of normal operations. We can deal with errors immediately, and the system will continue to operate at the same capacity following an error.

It does not mean that failures are always fatal. Rather, some capacity of the system will be reduced following a failure.

Errors are not fatal. They are part of the programming and can be occurred anytime.

Question - 30 : - What is the difference between Imperative, Functional and Reactive Programming?

Answer - 30 : -

Let's compare them to see the difference:

Imperative Programming: Imperative programming is a programming paradigm where each line of code is sequentially executed to produce the desired result. This programming paradigm forces programmers to write "how" a program will solve a certain task.

Functional Programming: Functional programming is a programming paradigm where we can set everything as a result of a function that avoids changing states and mutating data.

Reactive Programming: Reactive programming is a programming paradigm with asynchronous data streams or event streams. An event stream can be anything like keyboard inputs, button taps, gestures, GPS location updates, accelerometer, iBeacon etc. Here, we can listen to a stream and react to it according to the situation.


NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners