Skip to content

Commit a8d2cea

Browse files
committed
stack problem and solution
1 parent 54807bd commit a8d2cea

File tree

4 files changed

+118
-1
lines changed

4 files changed

+118
-1
lines changed

book/D-interview-questions-solutions.asc

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,18 @@ include::interview-questions/buy-sell-stock.js[tag=solution]
9595

9696
The runtime is `O(n)` and a space complexity of `O(1)`.
9797

98+
99+
98100
:leveloffset: +1
99101

100102
=== Solutions for Linked List Questions
101103
(((Interview Questions Solutions, Linked Lists)))
102104

103105
:leveloffset: -1
104106

107+
108+
109+
105110
[#linkedlist-q-merge-lists]
106111
include::content/part02/linked-list.asc[tag=linkedlist-q-merge-lists]
107112

@@ -170,14 +175,57 @@ include::interview-questions/linkedlist-same-data.js[tag=description]
170175
include::interview-questions/linkedlist-same-data.js[tag=solution]
171176
----
172177

173-
IMPLEMENTATION NOTES
178+
The function `findNextPointerIndex` is a helper to navigate each character on a linked list.
179+
Notice, that we increase the index (`i + 1`) on each iteration.
180+
If the index overflows, it moves to the next node and reset the index to zero.
181+
182+
174183

175184
*Complexity Analysis*:
176185

177186
- Time: `O(n)`. We go over all the characters on each lists
178187
- Space: `O(1)`. Only using pointers and no auxiliary data structures.
179188

180189

190+
191+
:leveloffset: +1
192+
193+
=== Solutions for Stack Questions
194+
(((Interview Questions Solutions, Stack)))
195+
196+
:leveloffset: -1
197+
198+
[#stack-q-valid-parentheses]
199+
include::content/part02/stack.asc[tag=stack-q-valid-parentheses]
200+
201+
We need to validate that brackets are properly opened and closed, following these rules:
202+
- An opened bracket must be close by the same type.
203+
- Open brackets mush be closed in the corrent order.
204+
205+
This is a parsing problem and usually stacks are a good candidates for them.
206+
207+
*Algorithm*:
208+
209+
- Create a mapping of opening bracket with its closing bracket
210+
- Iterate through the string
211+
- When we found an opening bracket, insert the corresponding closing bracket into the stack.
212+
- When we found a closing bracket, pop from the stack and make sure it correspond to the current character.
213+
- Check the stack is empty. If there's a leftover, it means that something didn't close properly.
214+
215+
*Implementation*:
216+
217+
[source, javascript]
218+
----
219+
include::interview-questions/valid-parentheses.js[tag=description]
220+
include::interview-questions/valid-parentheses.js[tag=solution]
221+
----
222+
223+
*Complexity Analysis*:
224+
225+
- Time: `O(n)`. We iterate over each character of the string.
226+
- Space: `O(n)`. We use an auxiliary stack.
227+
228+
181229
// [#linkedlist-q-TAG]
182230
// include::content/part02/linked-list.asc[tag=linkedlist-q-TAG]
183231

book/content/part02/stack.asc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,22 @@ Implementing the stack with an array and linked list would lead to the same time
8585
// end::table[]
8686

8787
It's not very common to search for values on a stack (other Data Structures are better suited for this). Stacks are especially useful for implementing <>.
88+
89+
90+
==== Interview Questions
91+
(((Interview Questions, Arrays)))
92+
93+
// tag::stack-q-valid-parentheses[]
94+
===== Validate Parentheses / Braces / Brackets
95+
96+
*ST-1*) _Given an string with 3 types of brakets: `()`, `{}`, and `[]`. Validate they are properly closed and opened._
97+
// end::stack-q-valid-parentheses[]
98+
99+
[source, javascript]
100+
----
101+
include::../../interview-questions/valid-parentheses.js[tag=description]
102+
// write you code here
103+
}
104+
----
105+
106+
_Solution: <>_
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// tag::description[]
2+
/**
3+
* Validate if the parentheses are opened and closed in the right order.
4+
*
5+
* @example
6+
* isParenthesesValid('(){}[]'); // true
7+
* isParenthesesValid('([{}])'); // true
8+
* isParenthesesValid('([{)}]'); // false
9+
*
10+
* @param {string} string - The string
11+
*/
12+
function isParenthesesValid(string) {
13+
// end::description[]
14+
// tag::solution[]
15+
const map = new Map([['(', ')'], ['{', '}'], ['[', ']']]);
16+
const stack = [];
17+
18+
for (const c of string) {
19+
if (map.has(c)) stack.push(map.get(c));
20+
else if (c !== stack.pop()) return false;
21+
}
22+
23+
return stack.length === 0;
24+
}
25+
// end::solution[]
26+
27+
module.exports = { isParenthesesValid };
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const { isParenthesesValid } = require('./valid-parentheses');
2+
3+
describe('Stack: Valid Parentheses', () => {
4+
it('should be valid', () => {
5+
expect(isParenthesesValid('()')).toEqual(true);
6+
});
7+
8+
it('should be valid with different kinds', () => {
9+
expect(isParenthesesValid('()[]{}')).toEqual(true);
10+
});
11+
12+
it('should be valid with different nested kinds', () => {
13+
expect(isParenthesesValid('([{}])')).toEqual(true);
14+
});
15+
16+
it('should not be valid if incomplete', () => {
17+
expect(isParenthesesValid('()(')).toEqual(false);
18+
});
19+
20+
it('should not be valid if invalid character is present', () => {
21+
expect(isParenthesesValid('()-')).toEqual(false);
22+
});
23+
});

0 commit comments

Comments
 (0)