-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode.html
More file actions
1788 lines (1706 loc) · 86.9 KB
/
LeetCode.html
File metadata and controls
1788 lines (1706 loc) · 86.9 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>LeetCode Top 150 Interview Quiz</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
body {
background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
}
.quiz-container {
background: white;
border-radius: 15px;
padding: 30px;
max-width: 800px;
width: 100%;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}
.quiz-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 30px;
padding-bottom: 20px;
border-bottom: 2px solid #eee;
}
.timer {
background: #ff6b6b;
color: white;
padding: 10px 20px;
border-radius: 25px;
font-weight: bold;
font-size: 1.2em;
}
.timer.warning {
background: #ffa502;
animation: pulse 1s infinite;
}
.timer.danger {
background: #ff4757;
animation: pulse 0.5s infinite;
}
@keyframes pulse {
0%, 100% { transform: scale(1); }
50% { transform: scale(1.05); }
}
.progress-info {
color: #666;
font-size: 1.1em;
}
.score-display {
background: #2ed573;
color: white;
padding: 10px 20px;
border-radius: 25px;
font-weight: bold;
}
.progress-bar {
width: 100%;
height: 10px;
background: #eee;
border-radius: 5px;
margin-bottom: 30px;
overflow: hidden;
}
.progress-fill {
height: 100%;
background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
transition: width 0.3s ease;
border-radius: 5px;
}
.category-badge {
display: inline-block;
background: #667eea;
color: white;
padding: 5px 15px;
border-radius: 15px;
font-size: 0.9em;
margin-bottom: 15px;
}
.difficulty-badge {
display: inline-block;
padding: 5px 15px;
border-radius: 15px;
font-size: 0.9em;
margin-left: 10px;
}
.difficulty-easy { background: #2ed573; color: white; }
.difficulty-medium { background: #ffa502; color: white; }
.difficulty-hard { background: #ff4757; color: white; }
.question {
font-size: 1.3em;
color: #2d3436;
margin-bottom: 25px;
line-height: 1.6;
}
.options {
display: flex;
flex-direction: column;
gap: 15px;
}
.option {
padding: 15px 20px;
border: 2px solid #ddd;
border-radius: 10px;
cursor: pointer;
transition: all 0.3s ease;
display: flex;
align-items: center;
}
.option:hover:not(.disabled) {
border-color: #667eea;
background: #f8f9ff;
}
.option.selected {
border-color: #667eea;
background: #667eea;
color: white;
}
.option.correct {
border-color: #2ed573;
background: #2ed573;
color: white;
}
.option.incorrect {
border-color: #ff4757;
background: #ff4757;
color: white;
}
.option.disabled {
cursor: not-allowed;
opacity: 0.7;
}
.option-letter {
width: 35px;
height: 35px;
border-radius: 50%;
background: #eee;
display: flex;
justify-content: center;
align-items: center;
margin-right: 15px;
font-weight: bold;
flex-shrink: 0;
}
.option.selected .option-letter,
.option.correct .option-letter,
.option.incorrect .option-letter {
background: rgba(255, 255, 255, 0.3);
}
.btn {
padding: 15px 40px;
border: none;
border-radius: 10px;
font-size: 1.1em;
cursor: pointer;
transition: all 0.3s ease;
margin-top: 25px;
}
.btn-primary {
background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
color: white;
}
.btn-primary:hover {
transform: translateY(-2px);
box-shadow: 0 5px 20px rgba(102, 126, 234, 0.4);
}
.btn-primary:disabled {
background: #ccc;
cursor: not-allowed;
transform: none;
}
.start-screen, .result-screen {
text-align: center;
padding: 40px 20px;
}
.start-screen h1, .result-screen h1 {
color: #2d3436;
margin-bottom: 20px;
font-size: 2.5em;
}
.start-screen p, .result-screen p {
color: #666;
margin-bottom: 30px;
font-size: 1.2em;
}
.result-score {
font-size: 4em;
color: #667eea;
margin: 30px 0;
}
.result-breakdown {
display: flex;
justify-content: center;
gap: 40px;
margin: 30px 0;
}
.breakdown-item {
text-align: center;
}
.breakdown-number {
font-size: 2em;
font-weight: bold;
}
.breakdown-label {
color: #666;
}
.correct-color { color: #2ed573; }
.incorrect-color { color: #ff4757; }
.skipped-color { color: #ffa502; }
.explanation {
background: #f8f9fa;
padding: 15px;
border-radius: 10px;
margin-top: 20px;
border-left: 4px solid #667eea;
text-align: left;
display: none;
}
.explanation.show {
display: block;
}
.btn-group {
display: flex;
gap: 15px;
justify-content: center;
flex-wrap: wrap;
}
.hidden {
display: none !important;
}
code {
background: #f1f2f6;
padding: 2px 6px;
border-radius: 4px;
font-family: 'Courier New', monospace;
}
</style>
</head>
<body>
<div class="quiz-container">
<!-- Start Screen -->
<div id="start-screen" class="start-screen">
<h1>🎯 LeetCode Top 150</h1>
<h2>Interview Quiz</h2>
<p>Test your knowledge of algorithms and data structures!</p>
<p>150 Questions • 30 seconds per question</p>
<div style="margin: 30px 0;">
<label for="question-count" style="color: #666; display: block; margin-bottom: 10px;">
Number of Questions:
</label>
<select id="question-count" style="padding: 10px 20px; font-size: 1em; border-radius: 10px; border: 2px solid #ddd;">
<option value="10">10 Questions</option>
<option value="25">25 Questions</option>
<option value="50">50 Questions</option>
<option value="100">100 Questions</option>
<option value="150" selected>150 Questions (Full)</option>
</select>
</div>
<button class="btn btn-primary" onclick="startQuiz()">Start Quiz</button>
</div>
<!-- Quiz Screen -->
<div id="quiz-screen" class="hidden">
<div class="quiz-header">
<div class="progress-info">
Question <span id="current-question">1</span> of <span id="total-questions">150</span>
</div>
<div class="timer" id="timer">30s</div>
<div class="score-display">Score: <span id="score">0</span></div>
</div>
<div class="progress-bar">
<div class="progress-fill" id="progress-fill"></div>
</div>
<div id="badges"></div>
<div class="question" id="question"></div>
<div class="options" id="options"></div>
<div class="explanation" id="explanation"></div>
<button class="btn btn-primary" id="next-btn" onclick="nextQuestion()" disabled>Next Question</button>
</div>
<!-- Result Screen -->
<div id="result-screen" class="result-screen hidden">
<h1>🏆 Quiz Complete!</h1>
<div class="result-score" id="final-score">0%</div>
<div class="result-breakdown">
<div class="breakdown-item">
<div class="breakdown-number correct-color" id="correct-count">0</div>
<div class="breakdown-label">Correct</div>
</div>
<div class="breakdown-item">
<div class="breakdown-number incorrect-color" id="incorrect-count">0</div>
<div class="breakdown-label">Incorrect</div>
</div>
<div class="breakdown-item">
<div class="breakdown-number skipped-color" id="skipped-count">0</div>
<div class="breakdown-label">Skipped</div>
</div>
</div>
<p id="result-message"></p>
<div class="btn-group">
<button class="btn btn-primary" onclick="restartQuiz()">Try Again</button>
<button class="btn btn-primary" onclick="reviewAnswers()">Review Answers</button>
</div>
</div>
</div>
<script>
// Quiz Questions - Conceptual questions about LeetCode Top 150 Interview Problems
const allQuestions = [
// Array / String Questions
{
category: "Array",
difficulty: "easy",
question: "What is the optimal time complexity for finding two numbers in an array that sum to a target value?",
options: ["O(n²)", "O(n)", "O(n log n)", "O(log n)"],
correct: 1,
explanation: "Using a hash map, we can achieve O(n) time complexity by storing seen values and checking if the complement exists."
},
{
category: "Array",
difficulty: "easy",
question: "When removing duplicates from a sorted array in-place, what technique is most commonly used?",
options: ["Binary search", "Two pointers", "Stack", "Recursion"],
correct: 1,
explanation: "The two-pointer technique efficiently handles this by maintaining a slow and fast pointer."
},
{
category: "Array",
difficulty: "medium",
question: "What is the time complexity of the Boyer-Moore Voting Algorithm for finding majority element?",
options: ["O(n²)", "O(n log n)", "O(n)", "O(1)"],
correct: 2,
explanation: "Boyer-Moore Voting Algorithm runs in O(n) time with O(1) space complexity."
},
{
category: "Array",
difficulty: "hard",
question: "For the 'Best Time to Buy and Sell Stock' problem, what's the key insight for an O(n) solution?",
options: ["Sort the prices first", "Track minimum price seen so far", "Use dynamic programming table", "Binary search for optimal days"],
correct: 1,
explanation: "Track the minimum price seen so far and calculate profit at each step - update max profit accordingly."
},
{
category: "Array",
difficulty: "medium",
question: "In the 'Jump Game' problem, what greedy approach works?",
options: ["Always jump maximum distance", "Track the farthest reachable index", "Use BFS from each position", "Sort jumps by distance"],
correct: 1,
explanation: "Track the farthest index you can reach; if current index exceeds this, return false."
},
{
category: "Array",
difficulty: "hard",
question: "What data structure is optimal for finding the median in a stream of numbers?",
options: ["Single sorted array", "Two heaps (max-heap and min-heap)", "Binary search tree", "Stack"],
correct: 1,
explanation: "Two heaps allow O(log n) insertion and O(1) median retrieval."
},
{
category: "Array",
difficulty: "easy",
question: "What's the space complexity of merging two sorted arrays into one?",
options: ["O(1)", "O(n)", "O(m+n)", "O(n²)"],
correct: 2,
explanation: "We need O(m+n) space to store all elements from both arrays."
},
{
category: "Array",
difficulty: "medium",
question: "In the 'Product of Array Except Self' problem, how can we achieve O(1) extra space (excluding output)?",
options: ["Use division", "Use prefix and suffix products in output array", "Use recursion", "Not possible"],
correct: 1,
explanation: "Store prefix products in output array, then multiply by suffix products in reverse pass."
},
{
category: "Array",
difficulty: "medium",
question: "What's the time complexity of the 'H-Index' problem using sorting?",
options: ["O(n)", "O(n log n)", "O(n²)", "O(log n)"],
correct: 1,
explanation: "Sorting takes O(n log n), then we iterate once through the sorted array."
},
{
category: "Array",
difficulty: "easy",
question: "For rotating an array by k positions, which approach uses O(1) space?",
options: ["Create new array", "Reverse segments", "Use queue", "Use recursion with array copy"],
correct: 1,
explanation: "Reverse entire array, then reverse first k elements, then reverse remaining - all in-place."
},
// Two Pointers Questions
{
category: "Two Pointers",
difficulty: "medium",
question: "In the 'Container With Most Water' problem, why do we move the pointer with smaller height?",
options: ["To increase width", "Smaller height limits area, moving might find taller line", "Random choice", "To decrease iterations"],
correct: 1,
explanation: "The smaller height is the bottleneck; moving it gives a chance to find a taller line that could increase area."
},
{
category: "Two Pointers",
difficulty: "medium",
question: "What's the time complexity of the 3Sum problem using sorting and two pointers?",
options: ["O(n)", "O(n²)", "O(n³)", "O(n log n)"],
correct: 1,
explanation: "O(n log n) for sorting + O(n²) for the nested loops = O(n²) overall."
},
{
category: "Two Pointers",
difficulty: "easy",
question: "In a sorted array, how do two pointers help find a pair with given sum?",
options: ["Start both at beginning", "Start both at end", "Start at opposite ends, move based on sum comparison", "Random positions"],
correct: 2,
explanation: "Start at opposite ends: if sum is too large, move right pointer left; if too small, move left pointer right."
},
{
category: "Two Pointers",
difficulty: "easy",
question: "What is the 'tortoise and hare' algorithm used for?",
options: ["Sorting arrays", "Detecting cycles in linked lists", "Binary search", "Graph traversal"],
correct: 1,
explanation: "Floyd's cycle detection uses two pointers moving at different speeds to detect cycles."
},
{
category: "Two Pointers",
difficulty: "medium",
question: "In the 'Trapping Rain Water' problem, what do two pointers track?",
options: ["Current and next element", "Left and right boundaries with max heights", "Start and end of water", "Two water containers"],
correct: 1,
explanation: "Track left and right pointers with their respective maximum heights to calculate trapped water."
},
// Sliding Window Questions
{
category: "Sliding Window",
difficulty: "medium",
question: "What's the key characteristic of a sliding window problem?",
options: ["Requires sorting", "Contiguous subarray/substring optimization", "Graph traversal", "Recursive solution only"],
correct: 1,
explanation: "Sliding window is used for problems involving contiguous sequences (subarrays or substrings)."
},
{
category: "Sliding Window",
difficulty: "medium",
question: "In 'Longest Substring Without Repeating Characters', what data structure tracks seen characters?",
options: ["Stack", "Queue", "Hash Set/Map", "Heap"],
correct: 2,
explanation: "A hash set or map efficiently tracks which characters are in the current window."
},
{
category: "Sliding Window",
difficulty: "hard",
question: "For 'Minimum Window Substring', when do we shrink the window?",
options: ["When window is too large", "When all required characters are found", "After each character", "Never"],
correct: 1,
explanation: "Once we have all required characters, shrink from left to find minimum valid window."
},
{
category: "Sliding Window",
difficulty: "medium",
question: "What's the time complexity of finding maximum sum subarray of size k?",
options: ["O(n*k)", "O(n)", "O(n²)", "O(k)"],
correct: 1,
explanation: "Sliding window allows O(n) by adding new element and removing old one as window slides."
},
{
category: "Sliding Window",
difficulty: "hard",
question: "In 'Substring with Concatenation of All Words', what makes it challenging?",
options: ["Words can be any length", "Words can overlap", "Words are of same length but must match exact concatenation", "Case sensitivity"],
correct: 2,
explanation: "All words have same length and must all appear exactly once in the substring."
},
// Matrix Questions
{
category: "Matrix",
difficulty: "medium",
question: "What's the time complexity of rotating a square matrix 90 degrees in-place?",
options: ["O(n)", "O(n²)", "O(n³)", "O(n log n)"],
correct: 1,
explanation: "We visit each element once or twice, giving O(n²) where n is the side length."
},
{
category: "Matrix",
difficulty: "medium",
question: "To rotate a matrix 90° clockwise, which operation sequence works?",
options: ["Reverse rows then transpose", "Transpose then reverse rows", "Reverse columns then transpose", "Transpose then reverse columns"],
correct: 1,
explanation: "Transpose the matrix, then reverse each row to achieve 90° clockwise rotation."
},
{
category: "Matrix",
difficulty: "medium",
question: "In spiral matrix traversal, how many direction changes occur for an m×n matrix?",
options: ["4", "m+n", "Varies with matrix size", "2*(m+n)"],
correct: 2,
explanation: "Direction changes depend on matrix dimensions; we change direction when hitting boundaries."
},
{
category: "Matrix",
difficulty: "easy",
question: "For setting matrix zeros, why is O(1) space solution tricky?",
options: ["Need to track rows and columns to zero out", "Matrix is immutable", "Need sorting first", "Recursive calls need space"],
correct: 0,
explanation: "Must mark rows/columns for zeroing without extra arrays - use first row/column as markers."
},
{
category: "Matrix",
difficulty: "medium",
question: "What's the valid approach for 'Game of Life' in-place update?",
options: ["Update cells randomly", "Use intermediate states to encode both old and new values", "Only update after full scan", "Update from corners"],
correct: 1,
explanation: "Encode both states in each cell (e.g., 2 for 0→1, 3 for 1→1) then decode in second pass."
},
// HashMap Questions
{
category: "HashMap",
difficulty: "easy",
question: "What's the average time complexity of hash map insertion?",
options: ["O(1)", "O(n)", "O(log n)", "O(n²)"],
correct: 0,
explanation: "Hash maps provide O(1) average case for insertion, deletion, and lookup."
},
{
category: "HashMap",
difficulty: "easy",
question: "In the 'Valid Anagram' problem, what does the hash map store?",
options: ["Word positions", "Character frequencies", "Sorted strings", "ASCII values"],
correct: 1,
explanation: "Count frequency of each character in both strings and compare."
},
{
category: "HashMap",
difficulty: "medium",
question: "For 'Group Anagrams', what serves as a good hash map key?",
options: ["First character", "String length", "Sorted string or character count tuple", "Last character"],
correct: 2,
explanation: "Sorted string or character frequency tuple uniquely identifies an anagram group."
},
{
category: "HashMap",
difficulty: "medium",
question: "In 'Longest Consecutive Sequence', why use a hash set?",
options: ["For sorting", "O(1) lookup to check if adjacent numbers exist", "For counting", "For removing duplicates only"],
correct: 1,
explanation: "Hash set allows O(1) lookup to check if n-1 or n+1 exists when finding sequences."
},
{
category: "HashMap",
difficulty: "easy",
question: "What data structure is best for checking if a pattern matches word mapping?",
options: ["Array", "Two hash maps (pattern→word and word→pattern)", "Stack", "Queue"],
correct: 1,
explanation: "Two maps ensure bijection: each pattern char maps to unique word and vice versa."
},
// Stack Questions
{
category: "Stack",
difficulty: "easy",
question: "Why is a stack ideal for validating parentheses?",
options: ["FIFO property", "LIFO property matches nested structure", "Fixed size", "Random access"],
correct: 1,
explanation: "LIFO (Last In First Out) naturally handles nested matching - most recent open bracket should match first."
},
{
category: "Stack",
difficulty: "medium",
question: "In 'Evaluate Reverse Polish Notation', what's pushed onto the stack?",
options: ["Operators only", "Operands only", "Both operators and operands", "Operands, operators trigger computation"],
correct: 3,
explanation: "Push numbers; when operator encountered, pop operands, compute, push result."
},
{
category: "Stack",
difficulty: "hard",
question: "What's the time complexity of 'Largest Rectangle in Histogram' using monotonic stack?",
options: ["O(n²)", "O(n)", "O(n log n)", "O(n³)"],
correct: 1,
explanation: "Each bar is pushed and popped at most once, giving O(n) complexity."
},
{
category: "Stack",
difficulty: "easy",
question: "In 'Min Stack', what's the trick for O(1) getMin()?",
options: ["Sort on each push", "Maintain separate stack tracking minimums", "Binary search", "Check all elements"],
correct: 1,
explanation: "Auxiliary stack stores minimum at each level, or store pairs of (value, currentMin)."
},
{
category: "Stack",
difficulty: "medium",
question: "What is a monotonic stack?",
options: ["Stack with fixed size", "Stack maintaining increasing or decreasing order", "Stack using recursion", "Stack of stacks"],
correct: 1,
explanation: "Monotonic stack maintains elements in sorted (increasing or decreasing) order."
},
// Linked List Questions
{
category: "Linked List",
difficulty: "easy",
question: "How do you find the middle of a linked list in one pass?",
options: ["Count nodes first", "Use slow and fast pointers", "Use recursion", "Use a hash map"],
correct: 1,
explanation: "Slow pointer moves 1 step, fast moves 2 steps; when fast reaches end, slow is at middle."
},
{
category: "Linked List",
difficulty: "medium",
question: "What's the key insight for reversing a linked list iteratively?",
options: ["Use extra array", "Track previous, current, and next pointers", "Use recursion only", "Swap node values"],
correct: 1,
explanation: "Maintain prev, curr, next pointers; redirect curr.next to prev, then advance all pointers."
},
{
category: "Linked List",
difficulty: "medium",
question: "To merge k sorted linked lists optimally, which data structure helps?",
options: ["Stack", "Queue", "Min-heap (priority queue)", "Hash map"],
correct: 2,
explanation: "Min-heap efficiently finds the smallest among k elements in O(log k) time."
},
{
category: "Linked List",
difficulty: "medium",
question: "In 'Copy List with Random Pointer', what's the interleaving approach?",
options: ["Use hash map for all nodes", "Insert copy after each original node", "Sort nodes first", "Use two passes with array"],
correct: 1,
explanation: "Insert copies next to originals (A→A'→B→B'), set random pointers, then separate lists."
},
{
category: "Linked List",
difficulty: "medium",
question: "How to detect where a cycle begins in a linked list?",
options: ["Use hash set only", "After detection, reset one pointer to head, move both at same speed", "Count total nodes", "Use BFS"],
correct: 1,
explanation: "After cycle detection, move one pointer to head; advance both at same speed; they meet at cycle start."
},
{
category: "Linked List",
difficulty: "hard",
question: "In 'LRU Cache', which data structures are combined?",
options: ["Array and stack", "Hash map and doubly linked list", "Tree and queue", "Graph and heap"],
correct: 1,
explanation: "Hash map for O(1) lookup, doubly linked list for O(1) insertion/deletion at any position."
},
// Binary Tree Questions
{
category: "Binary Tree",
difficulty: "easy",
question: "What's the time complexity of tree traversal (inorder, preorder, postorder)?",
options: ["O(log n)", "O(n)", "O(n²)", "O(n log n)"],
correct: 1,
explanation: "Each node is visited exactly once, giving O(n) time complexity."
},
{
category: "Binary Tree",
difficulty: "easy",
question: "Which traversal visits nodes in order: left, root, right?",
options: ["Preorder", "Inorder", "Postorder", "Level order"],
correct: 1,
explanation: "Inorder traversal visits left subtree, then root, then right subtree."
},
{
category: "Binary Tree",
difficulty: "medium",
question: "To check if a binary tree is symmetric, what do we compare?",
options: ["All node values", "Left subtree's left with right subtree's right, and vice versa", "Only leaf nodes", "Only root's children"],
correct: 1,
explanation: "Mirror check: left subtree's left = right subtree's right, left's right = right's left."
},
{
category: "Binary Tree",
difficulty: "medium",
question: "In 'Maximum Depth of Binary Tree', what's the recursive formula?",
options: ["max(left, right)", "1 + max(depth(left), depth(right))", "left + right", "min(left, right)"],
correct: 1,
explanation: "Depth = 1 (current node) + max of depths of left and right subtrees."
},
{
category: "Binary Tree",
difficulty: "medium",
question: "How do you flatten a binary tree to linked list (preorder)?",
options: ["Use array then rebuild", "Reverse postorder traversal with threading", "Morris traversal only", "BFS traversal"],
correct: 1,
explanation: "Process right-left-root (reverse preorder), threading each node to previously processed."
},
{
category: "Binary Tree",
difficulty: "hard",
question: "In 'Binary Tree Maximum Path Sum', why is it tricky?",
options: ["Path must include root", "Path can start and end at any nodes, may not pass through root", "Only leaves count", "Must use BFS"],
correct: 1,
explanation: "Maximum path might not go through root; track both max path through node and max path ending at node."
},
// Binary Search Tree Questions
{
category: "BST",
difficulty: "easy",
question: "What property defines a valid Binary Search Tree?",
options: ["All left nodes < root < all right nodes", "Balanced height", "Complete tree", "No duplicate values only"],
correct: 0,
explanation: "For every node, all values in left subtree are less, all in right subtree are greater."
},
{
category: "BST",
difficulty: "medium",
question: "What's the time complexity of search in a balanced BST?",
options: ["O(n)", "O(log n)", "O(1)", "O(n log n)"],
correct: 1,
explanation: "In a balanced BST, we eliminate half the remaining nodes at each step: O(log n)."
},
{
category: "BST",
difficulty: "medium",
question: "To validate a BST, what must each node check?",
options: ["Only immediate children", "All descendants respect min/max bounds from ancestors", "Only parent relationship", "Leaf nodes only"],
correct: 1,
explanation: "Pass down valid ranges; each node must be within bounds set by all ancestors."
},
{
category: "BST",
difficulty: "medium",
question: "What traversal of a BST produces sorted output?",
options: ["Preorder", "Postorder", "Inorder", "Level order"],
correct: 2,
explanation: "Inorder traversal (left-root-right) of BST visits nodes in ascending order."
},
{
category: "BST",
difficulty: "medium",
question: "To find kth smallest element in BST, which approach is most efficient?",
options: ["Sort all values", "Inorder traversal counting k nodes", "BFS", "Preorder traversal"],
correct: 1,
explanation: "Inorder traversal visits nodes in sorted order; stop after visiting k nodes."
},
// Graph Questions
{
category: "Graph",
difficulty: "medium",
question: "What's the difference between BFS and DFS time complexity on a graph?",
options: ["BFS is faster", "DFS is faster", "Both are O(V+E)", "Depends on graph density only"],
correct: 2,
explanation: "Both BFS and DFS visit each vertex and edge once: O(V+E) for adjacency list."
},
{
category: "Graph",
difficulty: "medium",
question: "Which algorithm finds the shortest path in an unweighted graph?",
options: ["DFS", "BFS", "Dijkstra's", "Bellman-Ford"],
correct: 1,
explanation: "BFS naturally finds shortest path in unweighted graphs due to level-by-level exploration."
},
{
category: "Graph",
difficulty: "medium",
question: "How do you detect a cycle in an undirected graph?",
options: ["Check if edges > vertices", "DFS/BFS finding visited node that's not parent", "Count connected components", "Sort edges"],
correct: 1,
explanation: "During traversal, if we reach an already-visited node that's not the immediate parent, there's a cycle."
},
{
category: "Graph",
difficulty: "medium",
question: "What data structure represents graph connections efficiently for sparse graphs?",
options: ["Adjacency matrix", "Adjacency list", "Edge list", "Incidence matrix"],
correct: 1,
explanation: "Adjacency list uses O(V+E) space, efficient for sparse graphs; matrix uses O(V²)."
},
{
category: "Graph",
difficulty: "medium",
question: "In 'Number of Islands', what represents an island?",
options: ["Connected region of 1s", "Any single 1", "Largest rectangle", "Diagonal connections"],
correct: 0,
explanation: "An island is a group of 1s connected horizontally or vertically."
},
{
category: "Graph",
difficulty: "hard",
question: "What is topological sorting used for?",
options: ["Sorting numbers", "Ordering tasks with dependencies (DAG)", "Finding shortest path", "Detecting cycles only"],
correct: 1,
explanation: "Topological sort orders vertices so all edges go from earlier to later in ordering (for DAGs)."
},
{
category: "Graph",
difficulty: "medium",
question: "Union-Find is optimal for which problem type?",
options: ["Shortest paths", "Dynamic connectivity / detecting connected components", "Sorting", "Tree traversal"],
correct: 1,
explanation: "Union-Find efficiently handles queries about connectivity between elements."
},
// Binary Search Questions
{
category: "Binary Search",
difficulty: "easy",
question: "What's the key requirement for binary search to work?",
options: ["Array must be unsorted", "Array must be sorted", "Array must have unique elements", "Array must be even length"],
correct: 1,
explanation: "Binary search requires a sorted array (or monotonic property) to eliminate half each iteration."
},
{
category: "Binary Search",
difficulty: "medium",
question: "In 'Search in Rotated Sorted Array', what's the key insight?",
options: ["Sort first", "At least one half is always sorted", "Use linear search", "Find rotation point first always"],
correct: 1,
explanation: "One of the two halves must be sorted; determine which and check if target is in that range."
},
{
category: "Binary Search",
difficulty: "medium",
question: "What's the time complexity of binary search?",
options: ["O(n)", "O(log n)", "O(n log n)", "O(1)"],
correct: 1,
explanation: "Each comparison eliminates half the search space: O(log n) iterations."
},
{
category: "Binary Search",
difficulty: "medium",
question: "In 'Find Peak Element', why does binary search work?",
options: ["Array is sorted", "Moving towards larger neighbor guarantees finding a peak", "Peak is always in middle", "Array has one peak only"],
correct: 1,
explanation: "If mid < mid+1, peak must exist on right side (including mid+1). Move towards increasing direction."
},
{
category: "Binary Search",
difficulty: "hard",
question: "For 'Median of Two Sorted Arrays', what's the time complexity of optimal solution?",
options: ["O(n + m)", "O(log(n+m))", "O(log(min(n,m)))", "O(n * m)"],
correct: 2,
explanation: "Binary search on the smaller array, finding correct partition: O(log(min(n,m)))."
},
// Heap / Priority Queue Questions
{
category: "Heap",
difficulty: "medium",
question: "What's the time complexity of building a heap from an array?",
options: ["O(n log n)", "O(n)", "O(log n)", "O(n²)"],
correct: 1,
explanation: "Bottom-up heap construction is O(n), more efficient than n insertions."
},
{
category: "Heap",
difficulty: "easy",
question: "What's the time complexity of inserting into a heap?",
options: ["O(1)", "O(log n)", "O(n)", "O(n log n)"],
correct: 1,
explanation: "Insert at end and bubble up through at most log n levels: O(log n)."
},
{
category: "Heap",
difficulty: "medium",
question: "For 'Kth Largest Element', which heap type and size is optimal?",
options: ["Max-heap of size n", "Min-heap of size k", "Max-heap of size k", "Min-heap of size n"],
correct: 1,
explanation: "Min-heap of size k: top element is kth largest; smaller elements naturally fall off."
},
{
category: "Heap",
difficulty: "hard",
question: "In 'Merge k Sorted Lists', what's the time complexity using a min-heap?",
options: ["O(nk)", "O(n log k)", "O(nk log k)", "O(n)"],
correct: 1,
explanation: "Each of n total elements is inserted/removed from heap of size k: O(n log k)."
},
{
category: "Heap",
difficulty: "medium",
question: "What property does a min-heap maintain?",
options: ["Parent ≥ children", "Parent ≤ children", "Left child < right child", "BST property"],
correct: 1,
explanation: "In a min-heap, each parent is smaller than or equal to its children."
},
// Backtracking Questions
{
category: "Backtracking",
difficulty: "medium",
question: "What characterizes a backtracking algorithm?",
options: ["Greedy choices only", "Try options, undo if they don't lead to solution", "Dynamic programming", "Always O(n) complexity"],
correct: 1,
explanation: "Backtracking explores paths, backs up when hitting dead ends, and tries alternatives."
},
{
category: "Backtracking",
difficulty: "medium",
question: "In 'N-Queens', what constraints must be checked before placing a queen?",
options: ["Row only", "Column only", "Row, column, and both diagonals", "Only other queens nearby"],
correct: 2,
explanation: "Queens attack along rows, columns, and diagonals; all must be checked."
},
{
category: "Backtracking",
difficulty: "medium",
question: "For generating all permutations of n elements, what's the time complexity?",
options: ["O(n)", "O(n²)", "O(n!)", "O(2^n)"],
correct: 2,
explanation: "There are n! permutations, and generating each takes O(n) work."
},
{
category: "Backtracking",
difficulty: "medium",
question: "In 'Combination Sum', how do we avoid duplicate combinations?",
options: ["Sort and skip duplicates, start from current index", "Always start from beginning", "Use hash set for results", "Random starting points"],
correct: 0,
explanation: "Sort input, avoid re-picking earlier elements (start from current index), skip consecutive duplicates."
},
{
category: "Backtracking",
difficulty: "medium",
question: "What's the key difference between combinations and permutations in backtracking?",
options: ["Same algorithm", "Combinations have fixed starting point, permutations swap all positions", "Combinations are faster", "No difference"],
correct: 1,
explanation: "Combinations pick from remaining elements (order doesn't matter); permutations consider all arrangements."
},
// Dynamic Programming Questions
{
category: "Dynamic Programming",
difficulty: "medium",
question: "What are the two main approaches to dynamic programming?",
options: ["BFS and DFS", "Top-down (memoization) and bottom-up (tabulation)", "Greedy and brute force", "Iterative and recursive only"],
correct: 1,
explanation: "Top-down uses recursion with memoization; bottom-up fills table from base cases."
},
{
category: "Dynamic Programming",
difficulty: "easy",
question: "In 'Climbing Stairs' (1 or 2 steps), what's the recurrence relation?",
options: ["dp[n] = dp[n-1]", "dp[n] = dp[n-1] + dp[n-2]", "dp[n] = 2 * dp[n-1]", "dp[n] = dp[n-1] * dp[n-2]"],
correct: 1,
explanation: "Ways to reach step n = ways to reach n-1 (then take 1 step) + ways to reach n-2 (then take 2 steps)."
},
{
category: "Dynamic Programming",
difficulty: "medium",
question: "What's the time complexity of the 'Longest Common Subsequence' problem?",
options: ["O(n)", "O(n²)", "O(m*n)", "O(2^n)"],
correct: 2,
explanation: "We fill an m×n table where m and n are lengths of the two strings."
},
{
category: "Dynamic Programming",
difficulty: "medium",
question: "In 'Coin Change', what does dp[i] represent?",