Skip to content
This repository was archived by the owner on Dec 12, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
56 changes: 2 additions & 54 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,57 +1,5 @@
# Code Problems

## Balanced Brackets
Code problems I have received in interviews and/or practiced prior to an interview. Most of the solutions here will be presented in JavaScript - but feel free to implement them in any language you are comfortable with.

Given a string consisting entirely of the characters ()[]{}, determine if it is "balanced". That is, every opening bracket must have a closing bracket of the same type following it, and the string in between the pair must also be balanced. For the purposes of the problem, an empty string should be considered balanced.

```
cd balanced-brackets
node index.js
# Every new line will output a new result
()[]{}(([])){[()][]}
())[]{}
[(])
```

## CSV Parsing

The input to this problem consists of a string of n comma-separated values, each value being an integer **or a string**. The required output is n consecutive lines, where line i contains the ith value of the input.
NOTICE – string may contain commas.

```
cd csv-parsing
cat input-1.csv | node index.js
cat input-2.csv | node index.js
cat input-3.csv | node index.js
```

## Anagram Detection

You are given two strings, a ‘parent’ string and a ‘query’ string respectively. Your task is to determine how many times the query string – *or an anagram of the query string* – appears in the parent string.

```
cd anagram-detection
cat input-1.txt | node index.js
cat input-2.txt | node index.js
```

## Spiral

You are given the dimension of a *h x w* grid filled with consecutive integers from left to right, top to bottom, starting with 1.

You are also given a starting position *r c*. The output should be the ordered list of integers obtained by spiralling outward in an anti-clockwise direction from row *r* column *c*, starting upward.

```
cd spiral
cat input-1.txt | node index.js
cat input-2.txt | node index.js
```

## Time Confusion

There are three watches, each giving a different time. One watch is x minutes behind the actual time. One watch is x minutes ahead of the actual time. From the time displayed on each watch, determine the actual time. If it is not possible, print "Look at the sun".

```
cd time-confusion
cat input-1.txt | node index.js
```
Feel free to add your own experiences as well.
3 changes: 3 additions & 0 deletions all-anagrams/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# All Anagrams

Given an input string, write a function that produces all possible anagrams of a string and outputs them as an array.
19 changes: 19 additions & 0 deletions all-anagrams/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var allAnagrams = function (string) {
var result = {};

// Using an immediately invoked named function for recursion
(function makeWord (word, remaining) {
// If there are no more remaining characters, break and set to true
// in the result object
if (!remaining) { return result[word] = true; }

// Loop through all the remaining letters and recurse slicing the character
// out of the remaining stack and into the solution word
for (var i = 0; i < remaining.length; i++) {
makeWord(word + remaining[i], remaining.substr(0, i) + remaining.substr(i + 1));
}
})('', string);

// Using the ES5 Object.keys to grab the all the keys as an array
return Object.keys(result);
};
53 changes: 53 additions & 0 deletions anagram-detection/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Anagram Detection

*Solutions will be evaluated based on correctness, clarity, efficiency and best practices.*

*You should use keyboard input and console output in all cases but parameter passing to functions are also acceptable. The input and output cases shown are for testing purposes only, and are not an accurate representation of the test data.*

You are given two strings, a ‘parent’ string and a ‘query’ string respectively. Your task is to determine how many times the query string – *or an anagram of the query string* – appears in the parent string.

NOTE: There are a range of solutions to this problem. With a little thought, you can massively improve the efficiency of your solution. The optimal solution runs almost instantly even for extremely large (1 million+ characters) parent and query strings.

**Input 1**

```
AdnBndAndBdaBn
dAn
```

**Output 1**

```
4
```

**Explanation 1**

The substrings are highlighted below.

```
**Adn**BndAndBdaBn
AdnB**ndA**ndBdaBn
AdnBn**dAn**dBdaBn
AdnBnd**And**BdaBn
```

**Input 2**

```
AbrAcadAbRa
cAda
```

**Output 2**

```
2
```

## Solution

```
cat input-1.txt | node index.js
cat input-2.txt | node index.js
```
53 changes: 53 additions & 0 deletions balanced-brackets/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Balanced Brackets

*Solutions will be evaluated based on correctness, clarity, efficiency and best practices.*

*You should use keyboard input and console output in all cases but parameter passing to functions are also acceptable. The input and output cases shown are for testing purposes only, and are not an accurate representation of the test data.*

Given a string consisting entirely of the characters ()[]{}, determine if it is "balanced". That is, every opening bracket must have a closing bracket of the same type following it, and the string in between the pair must also be balanced. For the purposes of the problem, an empty string should be considered balanced.

**Input 1**

```
()[]{}(([])){[()][]}
```

**Output 1**

```
balanced
```

**Input 2**

```
())[]{}
```

**Output 2**

```
not balanced
```

**Input 3**

```
[(])
```

**Output 3**

```
not balanced
```

## Solution

```
node index.js
# Every new line will output a new result
()[]{}(([])){[()][]}
())[]{}
[(])
```
26 changes: 14 additions & 12 deletions balanced-brackets/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
process.stdin.resume();
process.stdin.setEncoding('utf8');

// Use an object to map sets of brackets to their opposites
var brackets = {
')': '(',
'}': '{',
']': '['
'(': ')',
'{': '}',
'[': ']'
};

// On each input data chunk, process it using the balance checker
process.stdin.on('data', function (chunk) {
var stack = [];
// Process each line on input
chunk.split('').forEach(function (char) {
// Ignore the new lines
if (char === '\n') { return; }
// Check that the character is the bracket definitions
if (brackets[char] && brackets[char] === stack[stack.length - 1]) {
return stack.pop();
// Trim the chunk input
chunk = chunk.trim();
// Process every character on input
for (var i = 0; i < chunk.length; i++) {
if (brackets[stack[stack.length - 1]] === chunk[i]) {
stack.pop();
} else {
stack.push(chunk[i]);
}
stack.push(char);
});
}

console.log((stack.length ? 'not ' : '') + 'balanced');
});
9 changes: 9 additions & 0 deletions bubble-sort/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Bubble Sort

Bubble sort is the most basic sorting algorithm in all of Computer Sciencedom. It works by starting at the first element of an array and comparing it to the second element; if the first element is greater than the second element, it swaps the two. It then compares the second to the third, and the third to the fourth, and so on; in this way, the largest values "bubble" to the end of the array. Once it gets to the end of the array, it starts over and repeats the process until the array is sorted numerically.

Implement a function that takes an array and sorts it using this technique.

Extra credit: Optimization time! During any given pass, if no elements are swapped we can assume the list is sorted and can exit the function early. After this optimization, what is the time complexity of your algorithm?

Moar credits: Do you need to consider every element every time you iterate through the array? Make it happen, boss. Again: Has the time complexity of your algorithm changed?
27 changes: 27 additions & 0 deletions bubble-sort/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var bubbleSort = function (array) {
var switchArray = function (array, first, second) {
var temp = array[first];
array[first] = array[second];
array[second] = temp;
return array;
};

// Throw errors if the input was not an array
if (!Array.isArray(array)) {
throw new Error('Give me an array');
}

for (var i = 0; i < array.length; i++) {
if (array[i] > array[i + 1]) {
switchArray(array, i, i + 1);
var length = i;
while (length--) {
if (array[length] > array[length + 1]) {
switchArray(array, length, length + 1);
}
}
}
}

return array;
};
5 changes: 5 additions & 0 deletions common-string-filter/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Common String Filter

Write a function `f(a, b)` which takes two strings as arguments and returns a string containing only the characters found in both strings, in the order that they appeared in `a`.

Extra credit: Extend your function to handle more than two input strings.
17 changes: 17 additions & 0 deletions common-string-filter/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var commonStringFinder = function (string) {
var search = Array.prototype.slice.call(arguments, 1).join('').replace(/\W/g, ''),
result = '',
hash = {};

for (var j = 0; j < search.length; j++) {
hash[search[j]] = true;
}

for (var i = 0; i < string.length; i++) {
if (hash[string[i]] && !~result.indexOf(string[i])) {
result += string[i];
}
}

return result;
};
73 changes: 73 additions & 0 deletions csv-parsing/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# CSV Parsing

*Solutions will be evaluated based on correctness, clarity, efficiency and best practices.*

*You should use keyboard input and console output in all cases but parameter passing to functions are also acceptable. The input and output cases shown are for testing purposes only, and are not an accurate representation of the test data.*

The input to this problem consists of a string of *n* comma-separated values, each value being an integer **or a string**. The required output is *n* consecutive lines, where line *i* contains the *i*th value of the input.

NOTICE – string may contain commas (See **Input 2 and 3** below).

**Input 1**

```
2,6,3,2,5
```

**Output 1**

```
2
6
3
2
5
```

**Input 2**

```
"pears","apples","walnuts","grapes","cheese,cake"
```

**Output 2**

```
"pears"
"apples"
"walnuts"
"grapes"
"cheese,cake"
```

**Input 3**

```
1,"Que?","Kay?",2,"Si.","Sea? Kay, sea?","No, no, no. Que... ‘what’.",234,"Kay Watt?","Si, que ‘what’.","C.K. Watt?",3,"Yes!","comma,comma, comma , :)"
```

**Output 3**

```
1
"Que?"
"Kay?"
2
"Si."
"Sea? Kay, sea?"
"No, no, no. Que... ‘what’." 234
"Kay Watt?"
"Si, que ‘what’."
"C.K. Watt?"
3
"Yes!"
"comma,comma, comma , :)"
```

## Solution

```
cat input-1.csv | node index.js
cat input-2.csv | node index.js
cat input-3.csv | node index.js
```
9 changes: 9 additions & 0 deletions extend/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Extend

Extend is a function that copies all of the properties from a source object onto the target object.

A shallow-copy version of extend will copy all the top-level properties from a source object, copying the value of all primitive values (strings, numbers, etc.) but copying only a reference to objects and arrays.

A deep-copy version of extend will recursively copy all properties of the source object, giving the target a unique copy of any objects and arrays.

Write a shallow-copy version of extend, and then write a deep-copy version. Make your deep-copy extend handle an arbitrary number of source objects.
Loading