Coding practice
Angular Coding Questions
15 hands-on challenges with prompts and solution sketches for Angular interview rounds.
Challenge set
15 coding questions
1. Standalone Component
typescriptDeclare a standalone Angular component.
@Component({
selector: "app-hello",
standalone: true,
template: `<p>Hello {{ name }}</p>`,
})
export class HelloComponent {
name = "Angular";
} 2. Input Signal
typescriptAccept an input with input().
import { Component, input } from "@angular/core";
export class CardComponent {
title = input.required<string>();
} 3. Output Event
typescriptEmit an event with output().
import { output } from "@angular/core";
export class SaveButton {
saved = output<void>();
onClick() {
this.saved.emit();
}
} 4. HttpClient Get
typescriptFetch users with HttpClient.
constructor(private http: HttpClient) {}
load() {
return this.http.get<User[]>("/api/users");
} 5. Reactive Form
typescriptBuild a FormGroup with validators.
form = new FormGroup({
email: new FormControl("", [Validators.required, Validators.email]),
}); 6. Route Param
typescriptRead an id route param.
id = inject(ActivatedRoute).snapshot.paramMap.get("id"); 7. Async Pipe List
htmlRender an observable list with async pipe.
<li *ngFor="let user of users$ | async">{{ user.name }}</li> 8. Dependency Injection
typescriptInject a service with inject().
private readonly api = inject(ApiService); 9. Computed Signal
typescriptDerive a value with computed().
count = signal(0);
double = computed(() => this.count() * 2); 10. Structural Directive Usage
htmlConditionally render with @if.
@if (user) {
<p>{{ user.name }}</p>
} @else {
<p>Guest</p>
} 11. Track By
htmlUse track in @for.
@for (item of items; track item.id) {
<div>{{ item.label }}</div>
} 12. Interceptor Stub
typescriptAdd an auth header interceptor.
export const authInterceptor: HttpInterceptorFn = (req, next) => {
const token = inject(AuthService).token();
return next(req.clone({ setHeaders: { Authorization: `Bearer ${token}` } }));
}; 13. CanActivate Guard
typescriptProtect a route with a functional guard.
export const authGuard: CanActivateFn = () => {
return inject(AuthService).isLoggedIn() || inject(Router).parseUrl("/login");
}; 14. OnPush Change Detection
typescriptSet OnPush change detection.
@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
// ...
})
export class FastComponent {} 15. Destroy Ref Cleanup
typescriptClean up a subscription with DestroyRef.
const destroyRef = inject(DestroyRef);
const sub = source$.subscribe();
destroyRef.onDestroy(() => sub.unsubscribe());