Tech IndustrySep 25, 2019
NewtTvE03

I screw up my coding challenge, but can't find the mistake :(

Just bombed my coding challenge at a company. This was the task: You get a string with operation: 1) If it is a number push it to stack 2) if it is "DUP", then duplicate the one one top and push it to the stack 3) if it is "POP" then remove the one on top 4) if it is "+" add the two numbers on top of the stack and replace both with the result of the addition 5) if it is "-" then minus the two numbers on top (the second minus the first) of the stack and replace both with the result of the subtraction 6) if an error occurs, e.g. not enough numbers to add/subtract/pop, then return -1 Examples: Input: "3 DUP 5 - -" Output: -1 Input: "13 DUP 4 POP 5 DUP + DUP + -" Output: 7 Input: "5 6 + -" Output: -1 My result only got 66% correctness: https://jsbin.com/zifenofoha/1/edit?js,console function solution(S) { // write your code in JavaScript (Node.js 8.9.4) if (!S.length) { // early exit return -1; } const stack = new Array(); const pop = () => { if (!stack.length) { throw new Error('empty'); } return stack.pop(); } const duplicate = () => { try { const dup = pop(); stack.push(dup); stack.push(dup); } catch(e) { throw new Error(e); } }; const add = () => { try { const sum1 = pop(); const sum2 = pop(); stack.push(sum1 + sum2); } catch(e) { throw new Error(e); } }; const minus = () => { try { const min1 = pop(); const min2 = pop(); stack.push(min1 - min2); } catch(e) { throw new Error(e); } }; const ops = { "DUP": duplicate, "POP": pop, "+": add, "-": minus, }; const input = S.split(' '); input.forEach(x => { if (!isNaN(x)) { stack.push(Number(x)); } else { try { if (!ops[x]) { return -1; } ops[x](); } catch(e) { return -1; } } }); try { return pop(); } catch(e) { return -1; } }

Add a comment
Abbott 3001 Sep 25, 2019

HUdson River ? Do they really use the same problem from last year lol

Google kpdI0 Sep 25, 2019

You seem to assume there will always be a value to return on the stack. Couldn’t you have a valid string that pops all values off and leaves you with nothing to return? I don’t see that as part of the question spec.

New
tTvE03 OP Sep 25, 2019

Yes, I thought so. The last try-catch block should consider this You mean something like this? console.log(solution("1 2 + POP"));

New
tTvE03 OP Sep 25, 2019

I added a link where you can debug and see the code formatted.

Wayfair bat1227 Sep 25, 2019

Isn't the output of the second test case supposed to be 17?

New
xoogler420 Sep 26, 2019

" if it is "-" then minus the two numbers on top (the second minus the first) of the stack and replace both with the result of the subtraction" in your code you are doing first minus the second... I don't know if it's an error in your re-telling of the problem statement or in your code? Another thing is that your program doesn't handle whitespaces very well (of course I don't know about the test cases, because I don't have the exact problem statement) but for example for "3 5 POP " you return 0 instead of 3 Another thing that comes to mind is what you need to return if there's more than one element on the stack at the end. idk everyone is shooting in the dark without seeing the exact problem statement. Another example: Is input like "5 asdf 3" possible?