Bank of America Interview Experience

Recently, I appeared for a Java Full-Stack Interview with Bank of America. You can read the story if you are curious on what I have been asked. They have covered both frontend, backend, and databases. Frontend is Angular and backend is Java Spring Boot Microservices and Database SQL Server as primary.
Interview Duration — 30 mins but can go upto 45 mins.

Difference between constructor and ngOnInit?

constructor is a TypeScript feature not specific Angular feature. and its main purpose for injecting services same as in Java. and it is called when component object is created.

ngOnInit: ngOnInit is specific to Angular it is Angular lifecycle hook. where we initialize default values and call APIs and build business logic. and it is being called after constructor. means first we inject services in constructor and use those in ngOnInit.

can we call APIs and build business logic in constructor? yes.. we can. but the main purpose of constructor is for dependency injection not for business logic or API calls. It is bad practice and we will face problems. for eg. we are using @Input in our component. and we are trying to use that value in constructor you get undefined because by this time parent sends the value but Angular will not assign value. but same can be accessed in ngOnInit

@Input()
userId!: number;
constructor() {
console.log(this.userId);
}

here output still this.userId is undefined

ngOnInit() {
console.log(this.userId);
}

here you will get exact output that parent is passing

and imagine you have written all business logic and API calls in constructor only. how do you do testing? how do you write test cases? you will call unwanted methods.

consider you have two features if user is successfully added or not. if you are creating object you will do “new UserComponent()” where in this every operation triggers.

So each have their own purpose. constructor is used primarily for dependency injection and object initialization, whereas ngOnInit is part of Angular lifecycle and is used for component initialization like API calls, data loading, and business logic.

What are Angular Interceptors and how do they work?

Interceptors are the services mostly implements HttpInterceptor interface and it act as middleware between Angular application and backend APIs.

imagine you have 20 APIs and 7 services where for every API in every service you are individually writing authorization. and imagine you have to change authorization header. you have do it in every API. this is a tedious and time-consuming task. so we keep this functionality in one place. we can use interceptors where in interceptor interface we can provide authorization logic and use this in service. in case in future if authorization logic changes you can just change in interceptors instead of every API call.

I have given a simple example for implementing interceptor for authorization

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
@Injectable()
export class JwtInterceptor implements HttpInterceptor {
intercept(
req: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
const token = localStorage.getItem('token');
if (token) {
req = req.clone({
setHeaders: {
Authorization: `Bearer ${token}`
}
});
}
return next.handle(req);
}
}

Explain subject and behaviour subject?

Subject and Behaviour. Both are special types of RxJs Observables, which are both Observable means we can subscribe to it and Observer means it can emit values.

Majorly we use subject and behavioursubject to send and receive data from two components which are not related.

ok, if both are doing same work whats the point of both features right?

Let’s consider we have three components: menu-component, navbar-component and cart-component. we have a use case where we have to send data from menu-component to cart-component and but in menu-component we are passing via navbar-component. in case of subject on first subscribe you wont get any value. why it wont store any value. why? initially it carries values from menu-component but when it comes to navbar-component still u can get the value but from coming from navbar-component to cart-component it forgets because it wont store any previous values at all. if you subscribe in cart-component you will get the value undefined. but in case of behaviour subject it can store the previous value means when we are coming from menu-component -> navbar-component -> cart-component and subscribe in cart-component, you can get the value.

Syntax of subject:

const subject = new Subject<string>();

Syntax of behaviour subject:

const userSubject = new BehaviorSubject<string>('Guest');

How Can We Do Filtering in HTML?

We can implement filtering using a custom Angular Pipe by implementing the PipeTransform interface.

<input
type="text"
[(ngModel)]="searchText"
placeholder="Search User">
<ul>
<li *ngFor="let user of users | filter:searchText">
{{ user.name }}
</li>
</ul>

What are Spring Boot Starters and why do we use them?

Spring Boot Starters are pre-configured dependency packages. It bundles all the required libraries for a specific functionality.

Like, instead of adding multiple dependencies manually, we add starter dependency.

like without using starter dependency if we want to write an API call. we used to add multiple dependencies like Spring Core, Spring MVC, Tomcat, validation dependencies but now using starters we can just add spring-web starter dependency which includes all above dependencies.

What are Bean Scopes?

I have faced this question multiple times in the interviews.

So when we are creating a bean, how many instances of that bean should be created and how long that bean can live in Spring Boot is being decided by the type of bean we are creating.

We have four types of beans

Singleton Scope:

This is the default scope of a bean in Spring.

when we can write as

@Service
@Scope("singleton")
public class UserService {
}

or we can just write as

@Service
public class UserService {
}

second one is something we generally use.

So for singleton scope, only ONE object is being created in entire application, and we use the same object.

Prototype Scope:

Here, every time Spring is asked for the bean, a new bean is being created and both are different beans.

@Service
@Scope("prototype")
public class UserService {
}

Request Scope:

We create one object per request

@Component
@Scope(
value = WebApplicationContext.SCOPE_REQUEST,
proxyMode = ScopedProxyMode.TARGET_CLASS
)
public class RequestBean {
}

Session Scope:

Here, a bean is being created once in a session, and when you refresh the browser again, a new bean will be created.

@Component
@SessionScope
public class UserSession {
}

Application Scope:

One instance can be created in entire application

@Component
@ApplicationScope
public class AppConfig {
}

Final Thoughts:

You may or may not face the same questions, but have a glance at these before going to the interview. Please feel free to contact me in case of any queries or if you want clarification on any topic. I’ll be happy to take up your request.

All the best for your interview. Hope yall will be selected 😊

Thank you so much for reading.

Happy preparation 😊


Bank of America Interview Experience was originally published in Javarevisited on Medium, where people are continuing the conversation by highlighting and responding to this story.

This post first appeared on Read More