Mixed Interview questions Flashcards

(13 cards)

1
Q

What is the execution order of the following in Node.js?
process.nextTick(() => console.log(‘tick’));
setImmediate(() => console.log(‘immediate’));
setTimeout(() => console.log(‘timeout’), 0);
Promise.resolve().then(() => console.log(‘promise’));

A

Execution order:

process.nextTick() – runs immediately after current operation, before microtasks.

Promise.resolve().then() – runs after nextTick, before timers.

setTimeout() – runs in the timers phase.

setImmediate() – runs in the check phase (may run before or after setTimeout based on environment).

process.nextTick(() => console.log(‘tick’));
Promise.resolve().then(() => console.log(‘promise’));
setTimeout(() => console.log(‘timeout’), 0);
setImmediate(() => console.log(‘immediate’));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why can process.nextTick() starve the event loop?

A

Because nextTick callbacks run before every I/O phase. If you recursively schedule more process.nextTick(), it prevents the event loop from progressing, blocking timers, I/O, and microtasks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Given the following code, what is the likely output?

setTimeout(() => console.log(‘timeout’), 0);
setImmediate(() => console.log(‘immediate’));

A

It depends on the environment:

In a script: timeout may run first.

Inside an I/O callback: immediate usually runs first.
Because they run in different phases (setTimeout in timers, setImmediate in check), the order isn’t deterministic outside of I/O.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What happens when you nest microtasks and nextTicks inside a setTimeout?

setTimeout(() => {
Promise.resolve().then(() => console.log(‘promise inside’));
process.nextTick(() => console.log(‘nextTick inside’));
console.log(‘timeout’);
}, 0);

A

Output:

timeout

nextTick inside – after current callback, before microtasks.

promise inside – after nextTick, before leaving the timer phase.

timeout
nextTick inside
promise inside

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How does await relate to the event loop?

A

await is syntactic sugar for Promise.then(). It schedules the continuation as a microtask, which runs after the current task and process.nextTick, but before macrotasks like setTimeout.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the any type in TypeScript and when should it be avoided?

A

any disables type checking for the variable. It can be assigned any value and used in any way without compiler errors. It should be avoided in production code because it removes the safety guarantees of TypeScript.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the unknown type in TypeScript and how is it different from any?

A

unknown also accepts any value, but unlike any, it requires type checking before use. It’s a safer alternative to any when the type is not known at declaration time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the never type and where is it used?

A

never represents values that should never occur. It is used for:

Functions that never return (e.g., throw errors)

Exhaustiveness checks in switch statements
Useful for catching unhandled cases at compile time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Which is safer: any or unknown? Why?

A

unknown is safer because the compiler forces you to check the type before using it. any skips all checks and allows unsafe operations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How would you use never to enforce exhaustive switch-case logic?

A

type Shape = ‘circle’ | ‘square’;

function handle(shape: Shape) {
switch (shape) {
case ‘circle’:
// handle circle
break;
case ‘square’:
// handle square
break;
default:
const _exhaustiveCheck: never = shape; // error if new type is added
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Call() function

A
  1. call()

Invokes the function immediately.

You pass this as the first argument, followed by the actual arguments.

Example:

function greet(greeting, name) {
console.log(${greeting}, ${name}. From:, this.company);
}

const context = { company: ‘OpenAI’ };

greet.call(context, ‘Hello’, ‘Alice’);
// Output: “Hello, Alice. From: OpenAI”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Apply function

A

apply()

Just like call, but it takes arguments as a single array.

Example:

greet.apply(context, [‘Hi’, ‘Bob’]);
// Output: “Hi, Bob. From: OpenAI”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Bind function

A

bind()

Does not invoke the function immediately.

Instead, it returns a new function with this permanently bound.

Example:

const boundGreet = greet.bind(context, ‘Hey’);
boundGreet(‘Charlie’);
// Output: “Hey, Charlie. From: OpenAI”

You can reuse boundGreet as many times as you want.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly