-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestions.html
More file actions
1440 lines (1317 loc) · 53.3 KB
/
Questions.html
File metadata and controls
1440 lines (1317 loc) · 53.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tech Interview Questions - Frontend Developer</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
line-height: 1.6;
background-color: #f5f5f5;
position: relative;
}
.menu-button {
position: fixed;
top: 20px;
left: 20px;
z-index: 1001;
background: #3498db;
color: white;
border: none;
padding: 12px 16px;
border-radius: 8px;
cursor: pointer;
font-size: 18px;
box-shadow: 0 2px 8px rgba(0,0,0,0.2);
transition: background 0.3s;
}
.menu-button:hover {
background: #2980b9;
}
.sidebar {
position: fixed;
left: 0;
top: 0;
height: 100vh;
width: 300px;
background: white;
box-shadow: 2px 0 10px rgba(0,0,0,0.1);
transform: translateX(-100%);
transition: transform 0.3s ease;
z-index: 1000;
overflow-y: auto;
padding: 60px 0 20px 0;
}
.sidebar.open {
transform: translateX(0);
}
.sidebar h2 {
padding: 15px 20px;
color: #2c3e50;
font-size: 1.2em;
border-bottom: 2px solid #ecf0f1;
margin-bottom: 10px;
}
.sidebar ul {
list-style: none;
padding: 0;
}
.sidebar li {
margin: 0;
}
.sidebar a {
display: block;
padding: 12px 20px;
color: #34495e;
text-decoration: none;
transition: background 0.2s;
border-left: 3px solid transparent;
}
.sidebar a:hover {
background: #f8f9fa;
border-left-color: #3498db;
}
.sidebar .section-link {
font-weight: bold;
color: #2c3e50;
background: #ecf0f1;
padding: 10px 20px;
font-size: 0.9em;
text-transform: uppercase;
letter-spacing: 0.5px;
}
.content-wrapper {
max-width: 1200px;
margin: 0 auto;
padding: 20px 20px 20px 20px;
transition: margin-left 0.3s;
}
.content-wrapper.shifted {
margin-left: 300px;
}
.overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0,0,0,0.5);
opacity: 0;
visibility: hidden;
transition: opacity 0.3s, visibility 0.3s;
z-index: 999;
}
.overlay.active {
opacity: 1;
visibility: visible;
}
h1 {
color: #2c3e50;
border-bottom: 3px solid #3498db;
padding-bottom: 10px;
margin-top: 40px;
}
h2 {
color: #34495e;
margin-top: 30px;
font-size: 1.3em;
}
.question {
background: white;
padding: 20px;
margin: 15px 0;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
scroll-margin-top: 80px;
}
.question-title {
font-weight: bold;
color: #2c3e50;
font-size: 1.1em;
margin-bottom: 10px;
}
.answer {
color: #555;
margin-top: 10px;
padding-left: 15px;
border-left: 3px solid #3498db;
}
.code {
background: #f4f4f4;
padding: 10px;
border-radius: 4px;
font-family: 'Courier New', monospace;
overflow-x: auto;
margin: 10px 0;
}
ul {
margin: 10px 0;
padding-left: 25px;
}
li {
margin: 5px 0;
}
@media (max-width: 768px) {
.content-wrapper.shifted {
margin-left: 0;
}
}
</style>
</head>
<body>
<button class="menu-button" id="menuButton" aria-label="Toggle menu">☰</button>
<div class="overlay" id="overlay"></div>
<nav class="sidebar" id="sidebar">
<h2>Navigation</h2>
<ul>
<li><a href="#top" class="section-link">Top</a></li>
<li><a href="#react" class="section-link">React.js</a></li>
<li><a href="#q1">Q1: Class vs Functional Components</a></li>
<li><a href="#q2">Q2: React Hooks</a></li>
<li><a href="#q3">Q3: useEffect Hook</a></li>
<li><a href="#q4">Q4: React Performance</a></li>
<li><a href="#q5">Q5: Virtual DOM</a></li>
<li><a href="#q6">Q6: Reconciliation</a></li>
<li><a href="#q7">Q7: React Portals</a></li>
<li><a href="#typescript" class="section-link">TypeScript</a></li>
<li><a href="#q8">Q8: TypeScript Benefits</a></li>
<li><a href="#q9">Q9: Interfaces vs Types</a></li>
<li><a href="#q10">Q10: Generics</a></li>
<li><a href="#q11">Q11: Utility Types</a></li>
<li><a href="#redux" class="section-link">Redux & State</a></li>
<li><a href="#q12">Q12: Redux Architecture</a></li>
<li><a href="#q13">Q13: Redux Toolkit</a></li>
<li><a href="#q14">Q14: Async Actions</a></li>
<li><a href="#q15">Q15: Redux vs Context</a></li>
<li><a href="#javascript" class="section-link">JavaScript (ES6+)</a></li>
<li><a href="#q16">Q16: Closures</a></li>
<li><a href="#q17">Q17: var, let, const</a></li>
<li><a href="#q18">Q18: Promises & async/await</a></li>
<li><a href="#q19">Q19: Event Loop</a></li>
<li><a href="#q20">Q20: Destructuring & Spread</a></li>
<li><a href="#websockets" class="section-link">WebSockets</a></li>
<li><a href="#q21">Q21: WebSockets vs HTTP</a></li>
<li><a href="#q22">Q22: WebSocket Reconnection</a></li>
<li><a href="#testing" class="section-link">Testing (Playwright)</a></li>
<li><a href="#q23">Q23: Playwright Overview</a></li>
<li><a href="#q24">Q24: Playwright Async</a></li>
<li><a href="#webpack" class="section-link">Webpack</a></li>
<li><a href="#q25">Q25: Webpack Overview</a></li>
<li><a href="#q26">Q26: Code Splitting</a></li>
<li><a href="#api" class="section-link">API Integration</a></li>
<li><a href="#q27">Q27: Axios Interceptors</a></li>
<li><a href="#q28">Q28: REST API</a></li>
<li><a href="#ui" class="section-link">UI Libraries</a></li>
<li><a href="#q29">Q29: Chakra UI vs MUI</a></li>
<li><a href="#q30">Q30: CSS Modules</a></li>
<li><a href="#forms" class="section-link">Form Handling</a></li>
<li><a href="#q31">Q31: Formik & Yup</a></li>
<li><a href="#cicd" class="section-link">CI/CD</a></li>
<li><a href="#q32">Q32: GitHub Actions</a></li>
<li><a href="#performance" class="section-link">Performance</a></li>
<li><a href="#q33">Q33: React Optimization</a></li>
<li><a href="#q34">Q34: Tree Shaking</a></li>
<li><a href="#general" class="section-link">General Concepts</a></li>
<li><a href="#q35">Q35: CORS</a></li>
<li><a href="#q36">Q36: Auth vs Authorization</a></li>
<li><a href="#q37">Q37: Error Handling</a></li>
<li><a href="#q38">Q38: Responsive Design</a></li>
<li><a href="#q41">Q41: Cookies, Local & Session Storage</a></li>
<li><a href="#architecture" class="section-link">Architecture</a></li>
<li><a href="#q39">Q39: App Structure</a></li>
<li><a href="#q40">Q40: Best Practices</a></li>
</ul>
</nav>
<div class="content-wrapper" id="contentWrapper">
<div id="top"></div>
<h1>Frontend Developer Tech Interview Questions & Answers</h1>
<p><strong>Based on technologies from CV:</strong> React, TypeScript, Redux Toolkit, WebSockets, Playwright, Webpack, and more...</p>
<h1 id="react">React.js</h1>
<div class="question" id="q1">
<div class="question-title">Q1: Explain the difference between class components and functional components with hooks.</div>
<div class="answer">
<p><strong>Class Components:</strong> Use ES6 classes, have lifecycle methods (componentDidMount, componentDidUpdate, etc.), use 'this' keyword, and require more boilerplate code.</p>
<p><strong>Functional Components with Hooks:</strong> Simpler syntax, use hooks (useState, useEffect, etc.) for state and lifecycle, easier to test and understand, and are the recommended approach in modern React (React 16.8+).</p>
<p>Hooks allow functional components to have state and side effects, making them equivalent to class components but with cleaner code.</p>
</div>
</div>
<div class="question" id="q2">
<div class="question-title">Q2: What are React Hooks? Name the most commonly used hooks and their purposes.</div>
<div class="answer">
<p>React Hooks are functions that let you use state and other React features in functional components.</p>
<ul>
<li><strong>useState:</strong> Manages component state</li>
<li><strong>useEffect:</strong> Handles side effects (API calls, subscriptions, DOM manipulation)</li>
<li><strong>useContext:</strong> Accesses React context</li>
<li><strong>useReducer:</strong> Alternative to useState for complex state logic</li>
<li><strong>useMemo:</strong> Memoizes expensive calculations</li>
<li><strong>useCallback:</strong> Memoizes functions to prevent unnecessary re-renders</li>
<li><strong>useRef:</strong> Creates mutable references that persist across renders</li>
</ul>
</div>
</div>
<div class="question" id="q3">
<div class="question-title">Q3: Explain the useEffect hook and its dependency array.</div>
<div class="answer">
<p>useEffect runs after render and allows you to perform side effects. The dependency array controls when the effect runs:</p>
<ul>
<li><strong>No array:</strong> Runs after every render</li>
<li><strong>Empty array []:</strong> Runs only once after initial render (like componentDidMount)</li>
<li><strong>With dependencies [a, b]:</strong> Runs when a or b changes</li>
</ul>
<div class="code">
useEffect(() => {
// Side effect code
return () => {
// Cleanup function (like componentWillUnmount)
};
}, [dependency1, dependency2]);
</div>
</div>
</div>
<div class="question" id="q4">
<div class="question-title">Q4: How do you optimize React performance?</div>
<div class="answer">
<p>React performance optimization involves preventing unnecessary re-renders and expensive computations. Here are the key techniques:</p>
<p><strong>1. React.memo - Component Memoization</strong></p>
<p>React.memo is a higher-order component that memoizes the result of a component. It only re-renders if props have changed (shallow comparison).</p>
<div class="code">
const ExpensiveComponent = React.memo(({ name, count }) => {
console.log('Rendering ExpensiveComponent');
return (
<div>
<h2>{name}</h2>
<p>Count: {count}</p>
</div>
);
});
const ExpensiveComponentWithCustomComparison = React.memo(
({ name, count }) => {
return <div>{name}: {count}</div>;
},
(prevProps, nextProps) => {
return prevProps.name === nextProps.name && prevProps.count === nextProps.count;
}
);
</div>
<p><strong>When to use:</strong></p>
<ul>
<li>Components that render frequently with the same props</li>
<li>Components with expensive render logic</li>
<li>Components that receive primitive props (strings, numbers, booleans)</li>
</ul>
<p><strong>When NOT to use:</strong></p>
<ul>
<li>Props change frequently (memo overhead may outweigh benefits)</li>
<li>Props are complex objects/arrays (shallow comparison may not work as expected)</li>
<li>Component is already fast and simple</li>
</ul>
<p><strong>2. useMemo - Memoize Expensive Calculations</strong></p>
<p>useMemo memoizes the result of a computation and only recalculates when dependencies change.</p>
<div class="code">
function ExpensiveList({ items, filter }) {
const filteredItems = useMemo(() => {
console.log('Filtering items...');
return items.filter(item => item.category === filter);
}, [items, filter]);
const sortedItems = useMemo(() => {
console.log('Sorting items...');
return [...filteredItems].sort((a, b) => a.price - b.price);
}, [filteredItems]);
return (
<ul>
{sortedItems.map(item => <li key={item.id}>{item.name}</li>)}
</ul>
);
}
function FibonacciComponent({ n }) {
const fibonacci = useMemo(() => {
console.log('Calculating fibonacci...');
if (n <= 1) return n;
let a = 0, b = 1;
for (let i = 2; i <= n; i++) {
[a, b] = [b, a + b];
}
return b;
}, [n]);
return <div>Fibonacci({n}) = {fibonacci}</div>;
}
</div>
<p><strong>When to use:</strong></p>
<ul>
<li>Expensive calculations (filtering, sorting, mathematical operations)</li>
<li>Derived data that depends on props/state</li>
<li>Creating objects/arrays that are passed as props to memoized components</li>
</ul>
<p><strong>When NOT to use:</strong></p>
<ul>
<li>Simple calculations (overhead may be greater than benefit)</li>
<li>Dependencies change on every render (defeats the purpose)</li>
<li>Primitive values (no need to memoize)</li>
</ul>
<p><strong>3. useCallback - Memoize Callback Functions</strong></p>
<p>useCallback returns a memoized version of a callback that only changes if dependencies change. This prevents child components from re-rendering unnecessarily.</p>
<div class="code">
function ParentComponent({ userId }) {
const [count, setCount] = useState(0);
const [name, setName] = useState('');
const handleClick = useCallback(() => {
console.log('Button clicked');
setCount(c => c + 1);
}, []);
const handleSubmit = useCallback((data) => {
console.log('Submitting:', data);
fetch(`/api/users/${userId}`, {
method: 'POST',
body: JSON.stringify(data)
});
}, [userId]);
const handleChange = useCallback((e) => {
setName(e.target.value);
}, []);
return (
<div>
<ExpensiveChildComponent
onClick={handleClick}
onSubmit={handleSubmit}
/>
<input value={name} onChange={handleChange} />
</div>
);
}
const ExpensiveChildComponent = React.memo(({ onClick, onSubmit }) => {
console.log('Rendering ExpensiveChildComponent');
return (
<div>
<button onClick={onClick}>Click me</button>
<form onSubmit={onSubmit}>...</form>
</div>
);
});
</div>
<p><strong>When to use:</strong></p>
<ul>
<li>Callbacks passed to memoized child components (React.memo)</li>
<li>Callbacks used as dependencies in other hooks (useEffect, useMemo)</li>
<li>Callbacks passed to expensive child components</li>
</ul>
<p><strong>When NOT to use:</strong></p>
<ul>
<li>Callbacks that change on every render anyway (no benefit)</li>
<li>Simple callbacks that don't cause re-renders</li>
<li>Callbacks not passed to memoized components</li>
</ul>
<p><strong>Common Pitfalls and Best Practices:</strong></p>
<div class="code">
function BadExample({ items }) {
const [filter, setFilter] = useState('');
const filteredItems = items.filter(item => item.name.includes(filter));
const handleClick = () => {
console.log('clicked');
};
return (
<MemoizedChild
items={filteredItems}
onClick={handleClick}
/>
);
}
function GoodExample({ items }) {
const [filter, setFilter] = useState('');
const filteredItems = useMemo(() => {
return items.filter(item => item.name.includes(filter));
}, [items, filter]);
const handleClick = useCallback(() => {
console.log('clicked');
}, []);
return (
<MemoizedChild
items={filteredItems}
onClick={handleClick}
/>
);
}
</div>
<p><strong>Other Optimization Techniques:</strong></p>
<ul>
<li><strong>Code splitting:</strong> Lazy loading components with React.lazy() and Suspense to reduce initial bundle size</li>
<li><strong>Virtualization:</strong> Use libraries like react-window or react-virtualized for long lists (only render visible items)</li>
<li><strong>Avoid inline object/function creation in render:</strong> Creates new references each render, breaking memoization</li>
<li><strong>React DevTools Profiler:</strong> Identify performance bottlenecks and measure optimization impact</li>
<li><strong>Key optimization:</strong> Use stable keys in lists (avoid index as key when items can be reordered)</li>
<li><strong>State lifting:</strong> Lift state up only when necessary, keep state as local as possible</li>
<li><strong>Context optimization:</strong> Split contexts to prevent unnecessary re-renders</li>
</ul>
<p><strong>Performance Measurement:</strong></p>
<div class="code">
import { Profiler } from 'react';
function onRenderCallback(id, phase, actualDuration) {
console.log('Component:', id);
console.log('Phase:', phase);
console.log('Actual duration:', actualDuration);
}
function App() {
return (
<Profiler id="App" onRender={onRenderCallback}>
<YourComponent />
</Profiler>
);
}
</div>
</div>
</div>
<div class="question" id="q5">
<div class="question-title">Q5: What is the Virtual DOM and how does React use it?</div>
<div class="answer">
<p>The Virtual DOM is a JavaScript representation of the real DOM. React uses it to:</p>
<ul>
<li>Create a virtual representation of UI in memory</li>
<li>Compare new virtual DOM with previous version (diffing)</li>
<li>Calculate minimal changes needed (reconciliation)</li>
<li>Update only changed parts in the real DOM (batching)</li>
</ul>
<p>This approach is more efficient than directly manipulating the DOM because it batches updates and minimizes expensive DOM operations.</p>
</div>
</div>
<div class="question" id="q6">
<div class="question-title">Q6: Explain React's reconciliation algorithm.</div>
<div class="answer">
<p>Reconciliation is React's process of updating the DOM. Key points:</p>
<ul>
<li>React compares new element tree with previous one</li>
<li>Uses "diffing" algorithm to find differences</li>
<li>Updates only changed nodes, not entire tree</li>
<li>Uses keys to identify which items changed, were added, or removed</li>
<li>Keys should be stable, unique, and predictable</li>
</ul>
<p>Without keys, React may unnecessarily recreate components, causing performance issues and losing component state.</p>
</div>
</div>
<div class="question" id="q7">
<div class="question-title">Q7: What are React Portals and when would you use them?</div>
<div class="answer">
<p>Portals allow you to render children into a DOM node outside the parent component hierarchy.</p>
<div class="code">
import { createPortal } from 'react-dom';
function Modal({ children }) {
return createPortal(
children,
document.getElementById('modal-root')
);
}
</div>
<p>Use cases: modals, tooltips, dropdowns, or any UI that needs to break out of parent container constraints (e.g., overflow: hidden).</p>
</div>
</div>
<h1 id="typescript">TypeScript</h1>
<div class="question" id="q8">
<div class="question-title">Q8: What are the main benefits of TypeScript over JavaScript?</div>
<div class="answer">
<ul>
<li><strong>Type safety:</strong> Catches errors at compile time</li>
<li><strong>Better IDE support:</strong> Autocomplete, refactoring, navigation</li>
<li><strong>Self-documenting code:</strong> Types serve as documentation</li>
<li><strong>Refactoring confidence:</strong> Easier to refactor large codebases</li>
<li><strong>Better tooling:</strong> Enhanced debugging and development experience</li>
</ul>
</div>
</div>
<div class="question" id="q9">
<div class="question-title">Q9: Explain TypeScript interfaces vs types.</div>
<div class="answer">
<p><strong>Interfaces:</strong> Can be extended and merged, better for object shapes</p>
<div class="code">
interface User {
name: string;
age: number;
}
interface Admin extends User {
role: string;
}
</div>
<p><strong>Types:</strong> More flexible, can represent unions, intersections, primitives, and computed types</p>
<div class="code">
type Status = 'active' | 'inactive';
type UserOrAdmin = User | Admin;
</div>
<p>Generally: use interfaces for object shapes, types for unions/intersections/primitives.</p>
</div>
</div>
<div class="question" id="q10">
<div class="question-title">Q10: What are TypeScript generics and provide an example.</div>
<div class="answer">
<p>Generics allow you to create reusable components that work with multiple types.</p>
<div class="code">
function identity<T>(arg: T): T {
return arg;
}
interface ApiResponse<T> {
data: T;
status: number;
}
const userResponse: ApiResponse<User> = {
data: { name: 'John', age: 30 },
status: 200
};
</div>
<p>Generics provide type safety while maintaining flexibility.</p>
</div>
</div>
<div class="question" id="q11">
<div class="question-title">Q11: Explain TypeScript utility types (Partial, Pick, Omit, etc.).</div>
<div class="answer">
<p>Utility types transform existing types:</p>
<ul>
<li><strong>Partial<T>:</strong> Makes all properties optional</li>
<li><strong>Pick<T, K>:</strong> Selects specific properties</li>
<li><strong>Omit<T, K>:</strong> Removes specific properties</li>
<li><strong>Required<T>:</strong> Makes all properties required</li>
<li><strong>Readonly<T>:</strong> Makes all properties readonly</li>
</ul>
<div class="code">
interface User {
id: number;
name: string;
email: string;
}
type UserUpdate = Partial<User>; // All optional
type UserPreview = Pick<User, 'id' | 'name'>; // Only id and name
type UserWithoutId = Omit<User, 'id'>; // Everything except id
</div>
</div>
</div>
<h1 id="redux">Redux & State Management</h1>
<div class="question" id="q12">
<div class="question-title">Q12: Explain Redux architecture and its core concepts.</div>
<div class="answer">
<p>Redux is a predictable state container. Core concepts:</p>
<ul>
<li><strong>Store:</strong> Single source of truth holding application state</li>
<li><strong>Actions:</strong> Plain objects describing what happened</li>
<li><strong>Reducers:</strong> Pure functions that specify how state changes in response to actions</li>
<li><strong>Dispatch:</strong> Method to send actions to the store</li>
<li><strong>Selectors:</strong> Functions to extract specific data from state</li>
</ul>
<p>Data flow: Action → Dispatch → Reducer → New State → Components re-render</p>
</div>
</div>
<div class="question" id="q13">
<div class="question-title">Q13: What is Redux Toolkit and why is it preferred over plain Redux?</div>
<div class="answer">
<p>Redux Toolkit (RTK) is the official recommended way to write Redux logic. Benefits:</p>
<ul>
<li><strong>configureStore:</strong> Simplified store setup with good defaults</li>
<li><strong>createSlice:</strong> Reduces boilerplate for actions and reducers</li>
<li><strong>createAsyncThunk:</strong> Handles async actions easily</li>
<li><strong>Immer integration:</strong> Allows "mutating" logic in reducers</li>
<li><strong>Less boilerplate:</strong> Significantly less code than plain Redux</li>
</ul>
<div class="code">
import { createSlice } from '@reduxjs/toolkit';
const counterSlice = createSlice({
name: 'counter',
initialState: { value: 0 },
reducers: {
increment: (state) => {
state.value += 1; // Immer allows direct mutation
}
}
});
</div>
</div>
</div>
<div class="question" id="q14">
<div class="question-title">Q14: How do you handle async actions in Redux Toolkit?</div>
<div class="answer">
<p>Use createAsyncThunk for async operations:</p>
<div class="code">
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
export const fetchUser = createAsyncThunk(
'user/fetch',
async (userId) => {
const response = await fetch(`/api/users/${userId}`);
return response.json();
}
);
const userSlice = createSlice({
name: 'user',
initialState: { data: null, loading: false, error: null },
reducers: {},
extraReducers: (builder) => {
builder
.addCase(fetchUser.pending, (state) => {
state.loading = true;
})
.addCase(fetchUser.fulfilled, (state, action) => {
state.loading = false;
state.data = action.payload;
})
.addCase(fetchUser.rejected, (state, action) => {
state.loading = false;
state.error = action.error.message;
});
}
});
</div>
</div>
</div>
<div class="question" id="q15">
<div class="question-title">Q15: When would you use Redux vs local component state vs Context API?</div>
<div class="answer">
<ul>
<li><strong>Local state (useState):</strong> Component-specific data, form inputs, UI state (modals, toggles)</li>
<li><strong>Context API:</strong> Simple global state, theme, user auth, avoiding prop drilling</li>
<li><strong>Redux:</strong> Complex application state, shared data across many components, time-travel debugging, middleware needs (logging, async), large applications</li>
</ul>
<p>Rule of thumb: Start with local state, use Context for simple global state, use Redux for complex state management needs.</p>
</div>
</div>
<h1 id="javascript">JavaScript (ES6+)</h1>
<div class="question" id="q16">
<div class="question-title">Q16: Explain closures in JavaScript.</div>
<div class="answer">
<p>A closure is a function that has access to variables in its outer (enclosing) scope even after the outer function has returned.</p>
<div class="code">
function outerFunction(x) {
return function innerFunction(y) {
return x + y; // innerFunction has access to x
};
}
const addFive = outerFunction(5);
console.log(addFive(3)); // 8
</div>
<p>Common use cases: data privacy, function factories, event handlers, callbacks.</p>
</div>
</div>
<div class="question">
<div class="question-title">Q17: What is the difference between var, let, and const?</div>
<div class="answer">
<ul>
<li><strong>var:</strong> Function-scoped, hoisted, can be redeclared</li>
<li><strong>let:</strong> Block-scoped, hoisted but in TDZ (Temporal Dead Zone), cannot be redeclared, can be reassigned</li>
<li><strong>const:</strong> Block-scoped, hoisted but in TDZ, cannot be redeclared or reassigned (but object properties can be modified)</li>
</ul>
<p>Best practice: Use const by default, let when reassignment is needed, avoid var.</p>
</div>
</div>
<div class="question" id="q18">
<div class="question-title">Q18: Explain promises and async/await.</div>
<div class="answer">
<p><strong>Promises:</strong> Represent eventual completion of async operation</p>
<div class="code">
fetch('/api/data')
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));
</div>
<p><strong>Async/await:</strong> Syntactic sugar for promises, makes async code look synchronous</p>
<div class="code">
async function fetchData() {
try {
const response = await fetch('/api/data');
const data = await response.json();
console.log(data);
} catch (error) {
console.error(error);
}
}
</div>
</div>
</div>
<div class="question" id="q19">
<div class="question-title">Q19: What is the event loop and how does it work?</div>
<div class="answer">
<p>The event loop enables JavaScript's asynchronous behavior:</p>
<ul>
<li>JavaScript is single-threaded but uses an event loop for concurrency</li>
<li>Call stack executes synchronous code</li>
<li>Web APIs handle async operations (setTimeout, fetch, etc.)</li>
<li>Callback queue holds callbacks from completed async operations</li>
<li>Event loop moves callbacks from queue to stack when stack is empty</li>
</ul>
<p>This allows non-blocking I/O operations despite being single-threaded.</p>
</div>
</div>
<div class="question" id="q20">
<div class="question-title">Q20: Explain destructuring and spread operator.</div>
<div class="answer">
<p><strong>Destructuring:</strong> Extract values from arrays/objects</p>
<div class="code">
const { name, age } = user;
const [first, second] = array;
const { name: userName, ...rest } = user;
</div>
<p><strong>Spread operator:</strong> Expand arrays/objects</p>
<div class="code">
const newArray = [...oldArray, newItem];
const newObject = { ...oldObject, newProp: value };
</div>
</div>
</div>
<h1>WebSockets & Real-time Communication</h1>
<div class="question">
<div class="question-title">Q21: What are WebSockets and how do they differ from HTTP?</div>
<div class="answer">
<p><strong>HTTP:</strong> Request-response protocol, client initiates, stateless, one-way communication</p>
<p><strong>WebSockets:</strong> Full-duplex communication, persistent connection, server can push data, lower latency</p>
<div class="code">
const socket = new WebSocket('ws://example.com');
socket.onopen = () => {
socket.send('Hello Server');
};
socket.onmessage = (event) => {
console.log('Message:', event.data);
};
socket.onerror = (error) => {
console.error('Error:', error);
};
socket.onclose = () => {
console.log('Connection closed');
};
</div>
<p>Use cases: Chat applications, real-time notifications, live updates, collaborative editing.</p>
</div>
</div>
<div class="question" id="q22">
<div class="question-title">Q22: How would you handle WebSocket reconnection logic?</div>
<div class="answer">
<div class="code">
class WebSocketManager {
constructor(url) {
this.url = url;
this.socket = null;
this.reconnectAttempts = 0;
this.maxReconnectAttempts = 5;
this.reconnectDelay = 1000;
}
connect() {
this.socket = new WebSocket(this.url);
this.socket.onopen = () => {
this.reconnectAttempts = 0;
};
this.socket.onclose = () => {
if (this.reconnectAttempts < this.maxReconnectAttempts) {
setTimeout(() => {
this.reconnectAttempts++;
this.connect();
}, this.reconnectDelay * this.reconnectAttempts);
}
};
}
}
</div>
<p>Implement exponential backoff, connection state management, and message queuing for offline scenarios.</p>
</div>
</div>
<h1 id="testing">Testing (Playwright)</h1>
<div class="question" id="q23">
<div class="question-title">Q23: What is Playwright and how does it differ from other testing tools?</div>
<div class="answer">
<p>Playwright is an end-to-end testing framework. Advantages:</p>
<ul>
<li>Cross-browser testing (Chromium, Firefox, WebKit)</li>
<li>Auto-waiting for elements (no manual waits needed)</li>
<li>Network interception and mocking</li>
<li>Mobile device emulation</li>
<li>Parallel test execution</li>
<li>Better debugging with trace viewer</li>
<li>Modern async/await API</li>
</ul>
<div class="code">
import { test, expect } from '@playwright/test';
test('user login', async ({ page }) => {
await page.goto('https://example.com');
await page.fill('#username', 'user');
await page.fill('#password', 'pass');
await page.click('button[type="submit"]');
await expect(page.locator('.welcome')).toBeVisible();
});
</div>
</div>
</div>
<div class="question" id="q24">
<div class="question-title">Q24: How do you handle async operations and waiting in Playwright?</div>
<div class="answer">
<p>Playwright has built-in auto-waiting, but you can also use explicit waits:</p>
<div class="code">
await page.waitForSelector('.element');
await page.waitForLoadState('networkidle');
await page.waitForResponse(response => response.url().includes('/api/data'));
await expect(page.locator('.element')).toBeVisible({ timeout: 5000 });
</div>
<p>Playwright automatically waits for elements to be actionable before interacting with them.</p>
</div>
</div>
<h1 id="webpack">Webpack & Build Tools</h1>
<div class="question" id="q25">
<div class="question-title">Q25: What is Webpack and what problem does it solve?</div>
<div class="answer">
<p>Webpack is a module bundler that:</p>
<ul>
<li>Bundles JavaScript modules and dependencies</li>
<li>Transforms code (Babel, TypeScript, SASS)</li>
<li>Optimizes assets (minification, tree-shaking, code splitting)</li>
<li>Manages assets (images, fonts, CSS)</li>
<li>Enables hot module replacement (HMR) for development</li>
</ul>
<p>It takes modules with dependencies and generates static assets representing those modules.</p>
</div>
</div>
<div class="question" id="q26">
<div class="question-title">Q26: Explain code splitting in Webpack.</div>
<div class="answer">
<p>Code splitting allows you to split code into smaller chunks loaded on demand:</p>
<ul>
<li><strong>Entry point splitting:</strong> Multiple entry points</li>
<li><strong>Dynamic imports:</strong> Using import() for lazy loading</li>
<li><strong>SplitChunksPlugin:</strong> Automatically splits common dependencies</li>
</ul>
<div class="code">
// Dynamic import
const LazyComponent = React.lazy(() => import('./LazyComponent'));
// webpack.config.js
module.exports = {
optimization: {
splitChunks: {
chunks: 'all',
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'all',
},
},
},
},
};
</div>
</div>
</div>
<h1 id="api">API Integration & HTTP</h1>
<div class="question" id="q27">
<div class="question-title">Q27: How do you handle API requests with Axios interceptors?</div>
<div class="answer">
<div class="code">
import axios from 'axios';
const api = axios.create({
baseURL: 'https://api.example.com',
});
api.interceptors.request.use(
(config) => {
const token = localStorage.getItem('token');
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
},
(error) => Promise.reject(error)
);
api.interceptors.response.use(
(response) => response,
(error) => {
if (error.response?.status === 401) {
localStorage.removeItem('token');