Episode 7 — DSA with JavaScript / 7.14 — Stack
7.14 — Exercise Questions: Stack
E1. Basic Stack
Solution (JS)
class Stack{#i=[];push(v){this.#i.push(v);}pop(){return this.#i.pop();}peek(){return this.#i.at(-1);}isEmpty(){return!this.#i.length;}}
E2. Valid Parentheses
Solution (JS)
function vp(s){const st=[],m={')':'(',']':'[','}':'{'};for(const c of s){'([{'.includes(c)?st.push(c):st.pop()!==m[c]&&st.push('x');}return!st.length;}
E3. Min Stack
Solution (JS)
// See chapter implementation
E4. Next Greater Element
Solution (JS)
// See chapter implementation
E5. Evaluate Postfix
Solution (JS)
// See chapter implementation
E6. Sort Stack
Solution (JS)
// Pop all, insert in sorted position recursively
E7. Daily Temperatures
Solution (JS)
function dt(t){const r=Array(t.length).fill(0),st=[];for(let i=0;i<t.length;i++){while(st.length&&t[st.at(-1)]<t[i]){const j=st.pop();r[j]=i-j;}st.push(i);}return r;}
E8. Largest Rect Histogram
Solution (JS)
// See chapter implementation
E9. Decode String
Solution (JS)
// Stack of [string, count] pairs
E10. Stock Span
Solution (JS)
// Stack of indices, count days until larger
Additional exercise bank
EX-014
- Problem: Exercise #14.
- Difficulty: Easy
- Implementation: Both JS and C++.
EX-015
- Problem: Exercise #15.
- Difficulty: Easy
- Implementation: Both JS and C++.
EX-016
- Problem: Exercise #16.
- Difficulty: Easy
- Implementation: Both JS and C++.
EX-017
- Problem: Exercise #17.
- Difficulty: Easy
- Implementation: Both JS and C++.
EX-018
- Problem: Exercise #18.
- Difficulty: Easy
- Implementation: Both JS and C++.
EX-019
- Problem: Exercise #19.
- Difficulty: Easy
- Implementation: Both JS and C++.
EX-020
- Problem: Exercise #20.
- Difficulty: Easy
- Implementation: Both JS and C++.
EX-021
- Problem: Exercise #21.
- Difficulty: Easy
- Implementation: Both JS and C++.
EX-022
- Problem: Exercise #22.
- Difficulty: Easy
- Implementation: Both JS and C++.
EX-023
- Problem: Exercise #23.
- Difficulty: Easy
- Implementation: Both JS and C++.
EX-024
- Problem: Exercise #24.
- Difficulty: Easy
- Implementation: Both JS and C++.
EX-025
- Problem: Exercise #25.
- Difficulty: Easy
- Implementation: Both JS and C++.
EX-026
- Problem: Exercise #26.
- Difficulty: Easy
- Implementation: Both JS and C++.
EX-027
- Problem: Exercise #27.
- Difficulty: Easy
- Implementation: Both JS and C++.
EX-028
- Problem: Exercise #28.
- Difficulty: Easy
- Implementation: Both JS and C++.
EX-029
- Problem: Exercise #29.
- Difficulty: Easy
- Implementation: Both JS and C++.
EX-030
- Problem: Exercise #30.
- Difficulty: Easy
- Implementation: Both JS and C++.
EX-031
- Problem: Exercise #31.
- Difficulty: Easy
- Implementation: Both JS and C++.
EX-032
- Problem: Exercise #32.
- Difficulty: Easy
- Implementation: Both JS and C++.
EX-033
- Problem: Exercise #33.
- Difficulty: Easy
- Implementation: Both JS and C++.
EX-034
- Problem: Exercise #34.
- Difficulty: Easy
- Implementation: Both JS and C++.
EX-035
- Problem: Exercise #35.
- Difficulty: Easy
- Implementation: Both JS and C++.
EX-036
- Problem: Exercise #36.
- Difficulty: Easy
- Implementation: Both JS and C++.
EX-037
- Problem: Exercise #37.
- Difficulty: Easy
- Implementation: Both JS and C++.
EX-038
- Problem: Exercise #38.
- Difficulty: Easy
- Implementation: Both JS and C++.
EX-039
- Problem: Exercise #39.
- Difficulty: Easy
- Implementation: Both JS and C++.
EX-040
- Problem: Exercise #40.
- Difficulty: Easy
- Implementation: Both JS and C++.
EX-041
- Problem: Exercise #41.
- Difficulty: Easy
- Implementation: Both JS and C++.
EX-042
- Problem: Exercise #42.
- Difficulty: Easy
- Implementation: Both JS and C++.
EX-043
- Problem: Exercise #43.
- Difficulty: Easy
- Implementation: Both JS and C++.
EX-044
- Problem: Exercise #44.
- Difficulty: Easy
- Implementation: Both JS and C++.
EX-045
- Problem: Exercise #45.
- Difficulty: Easy
- Implementation: Both JS and C++.
EX-046
- Problem: Exercise #46.
- Difficulty: Easy
- Implementation: Both JS and C++.
EX-047
- Problem: Exercise #47.
- Difficulty: Easy
- Implementation: Both JS and C++.
EX-048
- Problem: Exercise #48.
- Difficulty: Easy
- Implementation: Both JS and C++.
EX-049
- Problem: Exercise #49.
- Difficulty: Easy
- Implementation: Both JS and C++.
EX-050
- Problem: Exercise #50.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-051
- Problem: Exercise #51.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-052
- Problem: Exercise #52.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-053
- Problem: Exercise #53.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-054
- Problem: Exercise #54.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-055
- Problem: Exercise #55.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-056
- Problem: Exercise #56.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-057
- Problem: Exercise #57.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-058
- Problem: Exercise #58.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-059
- Problem: Exercise #59.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-060
- Problem: Exercise #60.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-061
- Problem: Exercise #61.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-062
- Problem: Exercise #62.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-063
- Problem: Exercise #63.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-064
- Problem: Exercise #64.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-065
- Problem: Exercise #65.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-066
- Problem: Exercise #66.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-067
- Problem: Exercise #67.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-068
- Problem: Exercise #68.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-069
- Problem: Exercise #69.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-070
- Problem: Exercise #70.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-071
- Problem: Exercise #71.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-072
- Problem: Exercise #72.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-073
- Problem: Exercise #73.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-074
- Problem: Exercise #74.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-075
- Problem: Exercise #75.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-076
- Problem: Exercise #76.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-077
- Problem: Exercise #77.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-078
- Problem: Exercise #78.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-079
- Problem: Exercise #79.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-080
- Problem: Exercise #80.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-081
- Problem: Exercise #81.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-082
- Problem: Exercise #82.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-083
- Problem: Exercise #83.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-084
- Problem: Exercise #84.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-085
- Problem: Exercise #85.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-086
- Problem: Exercise #86.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-087
- Problem: Exercise #87.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-088
- Problem: Exercise #88.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-089
- Problem: Exercise #89.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-090
- Problem: Exercise #90.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-091
- Problem: Exercise #91.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-092
- Problem: Exercise #92.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-093
- Problem: Exercise #93.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-094
- Problem: Exercise #94.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-095
- Problem: Exercise #95.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-096
- Problem: Exercise #96.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-097
- Problem: Exercise #97.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-098
- Problem: Exercise #98.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-099
- Problem: Exercise #99.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-100
- Problem: Exercise #100.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-101
- Problem: Exercise #101.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-102
- Problem: Exercise #102.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-103
- Problem: Exercise #103.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-104
- Problem: Exercise #104.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-105
- Problem: Exercise #105.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-106
- Problem: Exercise #106.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-107
- Problem: Exercise #107.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-108
- Problem: Exercise #108.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-109
- Problem: Exercise #109.
- Difficulty: Medium
- Implementation: Both JS and C++.
EX-110
- Problem: Exercise #110.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-111
- Problem: Exercise #111.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-112
- Problem: Exercise #112.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-113
- Problem: Exercise #113.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-114
- Problem: Exercise #114.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-115
- Problem: Exercise #115.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-116
- Problem: Exercise #116.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-117
- Problem: Exercise #117.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-118
- Problem: Exercise #118.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-119
- Problem: Exercise #119.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-120
- Problem: Exercise #120.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-121
- Problem: Exercise #121.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-122
- Problem: Exercise #122.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-123
- Problem: Exercise #123.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-124
- Problem: Exercise #124.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-125
- Problem: Exercise #125.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-126
- Problem: Exercise #126.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-127
- Problem: Exercise #127.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-128
- Problem: Exercise #128.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-129
- Problem: Exercise #129.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-130
- Problem: Exercise #130.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-131
- Problem: Exercise #131.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-132
- Problem: Exercise #132.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-133
- Problem: Exercise #133.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-134
- Problem: Exercise #134.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-135
- Problem: Exercise #135.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-136
- Problem: Exercise #136.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-137
- Problem: Exercise #137.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-138
- Problem: Exercise #138.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-139
- Problem: Exercise #139.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-140
- Problem: Exercise #140.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-141
- Problem: Exercise #141.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-142
- Problem: Exercise #142.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-143
- Problem: Exercise #143.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-144
- Problem: Exercise #144.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-145
- Problem: Exercise #145.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-146
- Problem: Exercise #146.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-147
- Problem: Exercise #147.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-148
- Problem: Exercise #148.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-149
- Problem: Exercise #149.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-150
- Problem: Exercise #150.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-151
- Problem: Exercise #151.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-152
- Problem: Exercise #152.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-153
- Problem: Exercise #153.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-154
- Problem: Exercise #154.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-155
- Problem: Exercise #155.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-156
- Problem: Exercise #156.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-157
- Problem: Exercise #157.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-158
- Problem: Exercise #158.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-159
- Problem: Exercise #159.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-160
- Problem: Exercise #160.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-161
- Problem: Exercise #161.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-162
- Problem: Exercise #162.
- Difficulty: Hard
- Implementation: Both JS and C++.
EX-163
- Problem: Exercise #163.
- Difficulty: Hard
- Implementation: Both JS and C++.