Skip to content

Instantly share code, notes, and snippets.

@uzbekdev1
Created December 22, 2020 09:06
Show Gist options
  • Select an option

  • Save uzbekdev1/81d0a3f3feca631eb3b4aeb9f8c42d81 to your computer and use it in GitHub Desktop.

Select an option

Save uzbekdev1/81d0a3f3feca631eb3b4aeb9f8c42d81 to your computer and use it in GitHub Desktop.
Strange challenge - validation of strings and optimization of code [30 min for both]
Author's punctuation style is preserved.
Challenge:
- Assume most recent language standard are available (ES6)
- We expect good performance.
- If missing more requirements details, just make reasonable assumptions of your own.
- Solution must be simple and compact.
No defensive coding, no comments, no unrequested features.
Only one file 10-20 lines of code
- Work only inside Google Docs: no external editor/IDE/debugger, no copy-paste to/from such an editor.
We must see the flow of how you write the code.
Note: you have a total of 30 minutes for both questions!
(1)
Implement function verify(text) which verifies whether parentheses within text are correctly nested.
You need to consider three kinds: (), [], <>.
Examples:
verify("---(++++)----") -> 1
verify("") -> 1
verify("before ( middle []) after ") -> 1
verify(") (") -> 0
verify("} {") -> 1 //no, this is not a mistake.
verify("<( >)") -> 0
verify("( [ <> () ] <> )") -> 1
verify(" ( [)") -> 0
Answer:
/**
* @class ProjectAPI
*/
class ProjectAPI {
/**
* @constructor
*/
constructor() {}
/**
* @function verify
*
* @param string
* @returns {number}
*/
static verify(string) {
let patternsString = '()[]<>',
stackArray = [],
character,
bracePosition;
for (let i = 0; character = string[i]; i++) {
bracePosition = patternsString.indexOf(character);
if (bracePosition === -1) {
continue;
}
if (bracePosition % 2 === 0) {
stackArray.push(bracePosition + 1);
}
else {
if (stackArray.length === 0 || stackArray.pop() !== bracePosition) {
return 0;
}
}
}
return +(stackArray.length === 0);
}
/**
* @function outputData
*
* @param string
*/
static outputData(string) {
return console.log(string);
}
}
(2)
Problem
Simplify the implementation below as much as you can.
Even better if you can also improve performance as part of the simplification!
FYI: This code is over 35 lines and over 300 tokens, but it can be written in 5 lines and in less than 60 tokens.
Function on the next page.
function func(s, a, b)
{
var match_empty=/^$/ ;
if (s.match(match_empty))
{
return -1;
}
else
{
var i=s.length-1;
var aIndex=-1;
var bIndex=-1;
while ((aIndex==-1) && (bIndex==-1) && (i>=0))
{
if (s.substring(i, i+1) == a)
aIndex=i;
if (s.substring(i, i+1) == b)
bIndex=i;
i--;
}
if (aIndex != -1)
{
if (bIndex == -1)
return aIndex;
else
return Math.max(aIndex, bIndex);
}
else
{
if (bIndex != -1)
return bIndex;
else
return -1;
}
}
};
Answer:
let func = (s, a, b) => {
let matchEmpty = /^$/;
if (s.match(matchEmpty)) {
return -1;
}
for (let i = s.length - 1, aIndex = -1, bIndex = -1; aIndex == -1 && bIndex ==- 1 && i >= 0;) {
s.substring(i, i + 1) == a && (aIndex = i);
s.substring(i, i + 1) == b && (bIndex = i);
i--;
}
if (aIndex != -1) {
return bIndex == -1 ? aIndex : Math.max(aIndex, bIndex);
}
else {
return bIndex != -1 ? bIndex : -1;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment