Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
4425384
Four new exercises with solution in Java added.
AntonioRedondo May 17, 2015
a2f38b3
Title corrected.
AntonioRedondo May 18, 2015
5696d89
Merge pull request #145 from AntonioRedondo/master
blakeembrey May 18, 2015
e7fe9b4
Create bubbleSort.java
Widea Aug 6, 2015
9c5e2f8
Delete bubbleSort.java
Widea Aug 7, 2015
e0e3d4e
Create bubbleSort.java
Widea Aug 7, 2015
9fbb72f
Rename bubbleSort.java to BubbleSort.java
Widea Aug 7, 2015
4f05696
updated BinarySearchTree.java
Widea Aug 9, 2015
d37dd99
Update CombineTwoStrings.java
Widea Aug 10, 2015
223fe0d
Merge pull request #147 from Widea/widea-local
blakeembrey Aug 11, 2015
e144126
Merge pull request #143 from stezu/master
blakeembrey Aug 11, 2015
d3144db
Correct array pair sum
blakeembrey Aug 11, 2015
c94b234
Update RemoveDuplicatesFromString.java
Widea Aug 12, 2015
a6d971a
Merge pull request #1 from Widea/widea
Widea Aug 12, 2015
c2aae31
Create treelevelorderprint
Widea Aug 12, 2015
3575aa1
Updated TreeLevelOrderPrint.java
Widea Aug 14, 2015
e89aa36
Merge pull request #149 from Widea/Widea-1
blakeembrey Aug 14, 2015
7364c43
Create BinarySearchTree.java
Widea Aug 23, 2015
efa9be9
Update to handle an edge case
Widea Aug 24, 2015
d323d4e
Merge pull request #150 from Widea/patch-1
blakeembrey Aug 24, 2015
54de34d
Merge pull request #2 from Widea/Widea
Widea Aug 26, 2015
1329147
Merge pull request #151 from Widea/Widea
blakeembrey Aug 26, 2015
2cf7d75
Added problem description
cjjavellana Sep 20, 2015
b1012cd
Added solution to skiing in singapore problem
cjjavellana Sep 20, 2015
3ca118f
Merge pull request #152 from cjjavellana/master
blakeembrey Sep 20, 2015
acaa87e
Added spreadsheet problem set and solution; Fixed spelling errors in …
cjjavellana Sep 21, 2015
4c064a4
Removed personal comments
cjjavellana Sep 22, 2015
9c65715
Removed problem description from README and removed archive dependencies
cjjavellana Sep 22, 2015
165f92f
Merge pull request #153 from cjjavellana/master
blakeembrey Sep 23, 2015
fe9c948
Added solution to sum-of-array-plus-one in python
saurabhjn76 Sep 25, 2015
e4acc07
Added solution to multiples-of-3-and-5 in C.
saurabhjn76 Sep 25, 2015
22fd658
Merge pull request #154 from saurabhjn76/master
blakeembrey Sep 26, 2015
7f3e713
Added solution to bubble-sort in python.
saurabhjn76 Sep 27, 2015
0e1acad
Added solution to Finding-missing-element in java
saurabhjn76 Sep 28, 2015
e74f475
Added solution to find-missing-element in C.
saurabhjn76 Sep 28, 2015
16f058d
Correct `array-pair-sum` solution in JavaScript
blakeembrey Sep 27, 2015
e146498
Fix spelling of “entirely” in brackets problem
blakeembrey Sep 27, 2015
d8cced6
Improved compute time of combine strings solution
blakeembrey Sep 28, 2015
1bd4677
Fix JS `array-pair-sum` tests
blakeembrey Sep 29, 2015
c08967c
Merge pull request #155 from saurabhjn76/master
blakeembrey Oct 3, 2015
031c984
Create Fibonacci.java
Widea Oct 22, 2015
ca85ac8
Merge pull request #160 from Widea/Widea-fib
blakeembrey Oct 22, 2015
38ed64b
making example cases same as tests
lucaslin Nov 7, 2015
f8bf2e4
Merge pull request #162 from lucaslin/patch-1
blakeembrey Nov 7, 2015
6882f10
Add note on contributing to repo
blakeembrey Dec 21, 2015
7edcdfd
Added shortest Fizz buzz python solution
faruzzy Jan 6, 2016
b3f7be1
Merge pull request #163 from faruzzy/master
blakeembrey Jan 6, 2016
2744e3a
Create closetsum.py
pkmm91 Jan 12, 2016
c66fc97
closest sum
pkmm91 Jan 13, 2016
9b6d1aa
Flatten array ruby solution.
jmera Jan 26, 2016
b4a3139
Add factorial solution in Elixir
Jan 31, 2016
c1164a7
Merge pull request #166 from YoussefKababe/master
blakeembrey Jan 31, 2016
8889303
Merge pull request #165 from jmera/master
blakeembrey Jan 31, 2016
58d64b2
BubbleSort: Fix to reduce number of iterations for sorted array as in…
jayeshcp May 3, 2016
9fdbb66
Add string permutations solution in Python with 2 algorithms (#170)
alfre2v Aug 4, 2016
a8cc2fe
feat(convert-array): Add javascript solution for convert-array proble…
vasergen Aug 14, 2016
42b70ba
Move Java fibonacci solution into `java` solutions
blakeembrey Oct 1, 2016
968467c
Improve the palindrome solution (way better now!)
blakeembrey Oct 1, 2016
f046083
Disable linting of JS, update deps
blakeembrey Jan 9, 2017
0570b7b
Add PHPUnit test suite (#176)
caoglish Jan 10, 2017
6156b60
Add more php unit test cases and fix php solution (#177)
caoglish Jan 11, 2017
a448e31
Update some phpunit test cases (#178)
caoglish Jan 12, 2017
52dd522
String permutations in Go (#179)
alfre2v Jan 28, 2017
d731921
Add stack implementation in Go (#180)
itsanna Feb 8, 2017
90fe886
Shorter shortest-fizz-buzz for python{2,3} (#181)
burz Apr 5, 2017
15734b1
Add Triplebyte referral link
blakeembrey Nov 6, 2019
8eedb7e
Update README documentation
blakeembrey Nov 6, 2019
71371cb
Test bubble sort solution in JS (#185)
SpiralOutDotEu Nov 6, 2019
ce8eb51
Test balanced brackets in JS (#184)
SpiralOutDotEu Nov 6, 2019
eab8af2
Test combine two strings in JS (#186)
SpiralOutDotEu Nov 6, 2019
05fc3da
Update multiples-of-3-and-5.py (#187)
dhruvs009 Nov 6, 2019
fad508a
Adding transitive dependencies problem
Oct 7, 2015
a2c4480
Make fizz buzz even shorter (#189)
rgambord Dec 12, 2019
fb40ead
Add "shop in candy store" (#191)
hiteshrew Oct 26, 2020
c473ac5
-3 characters (#192)
ElijahCirioli Mar 17, 2022
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
Expand Up @@ -2,3 +2,4 @@
.idea
node_modules
*.o
vendor
31 changes: 0 additions & 31 deletions .jshintrc

This file was deleted.

18 changes: 13 additions & 5 deletions Readme.md → README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
# Code Problems

This is my repo full of code problems that I have completed prior to or during an interview, as well as general problem snippets. I hope that all these problems (and solutions) are useful to others for practice and review. Feel free to contribute any solutions and optimisations, and add your own problems that you find as well, as I would love to see them.
> Looking for a new job? Take Triplebyte's [quiz](https://triplebyte.com/a/QW7kzKo/d) and have top tech companies pitch you!

This is an incomplete repo full of code problems used prior to or during an interview. I hope these problems will be useful for you to practice and review. If you have practiced on or designed an interesting problem, we'd love to see it! Open a PR or issue with your suggestions or optimizations.

## Problems

### Sorting Algorithms
View [all problems](./problems), or start here:

**Sorting Algorithms**

* [Quick Sort](https://github.com/blakeembrey/code-problems/tree/master/problems/quick-sort)
* [Bubble Sort](https://github.com/blakeembrey/code-problems/tree/master/problems/bubble-sort)
* [Merge Sort](https://github.com/blakeembrey/code-problems/tree/master/problems/merge-sort)
* [Selection Sort](https://github.com/blakeembrey/code-problems/tree/master/problems/selection-sort)
* [Insertion Sort](https://github.com/blakeembrey/code-problems/tree/master/problems/insertion-sort)

### Data Structures
**Data Structures**

* [Stack](https://github.com/blakeembrey/code-problems/tree/master/problems/stack)
* [Queue](https://github.com/blakeembrey/code-problems/tree/master/problems/queue)
* [Linked List](https://github.com/blakeembrey/code-problems/tree/master/problems/linked-list)
* [Binary Search Tree Check](https://github.com/blakeembrey/code-problems/tree/master/problems/binary-search-tree-check) *Not an implementation, checks for a valid implementation*

### DOM Problems
**DOM Problems**

* [Get Elements By Class Name](https://github.com/blakeembrey/code-problems/tree/master/problems/get-elements-by-class-name)

### Shortest Code Problems
**Shortest Code Problems**

* [Shortest Fizz Buzz](https://github.com/blakeembrey/code-problems/tree/master/problems/shortest-fizz-buzz)

Expand All @@ -33,3 +37,7 @@ This is my repo full of code problems that I have completed prior to or during a
npm install # Installs `mocha` and any other dependencies needed to run
npm test # Runs the testing scripts
```

## Contributing

If you have a problem or solution that's not currently not included, please open an issue or pull request! If you're including new content, please make sure you have permission to publish the content you are adding. Under no circumstance should you copy problems online without a visible license or attribution, or from coding interview websites such as [Codility](https://codility.com/).
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"phpunit/phpunit": "5.5.*"
}
}
12 changes: 4 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
{
"name": "code-problems",
"version": null,
"private": true,
"description": "Repository full of code problems and common solutions.",
"main": null,
"dependencies": {},
"description": "Repository full of code problems and common solutions",
"devDependencies": {
"mocha": "~1.16.2",
"jshint": "~2.4.0",
"chai": "~1.8.1"
"chai": "^3.5.0",
"mocha": "^3.2.0"
},
"scripts": {
"test": "jshint solutions/javascript && mocha tests/javascript"
"test": "mocha tests/javascript"
},
"repository": {
"type": "git",
Expand Down
6 changes: 6 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites >
<testsuite name="code-problom test suites">
<directory suffix="Test.php">tests/php</directory>
</testsuite>
</testsuites>
5 changes: 3 additions & 2 deletions problems/array-pair-sum/Readme.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# Array Pair Sum

Given an integer array, output all pairs that sum up to a specific value k. Consider the fact that the same number can add up to `k` with its duplicates in the array.
Given an integer array, output all distinct pairs that sum up to a specific value k. Consider the fact that the same number can add up to `k` with its duplicates in the array.

> For example the array is [1, 1, 2, 3, 4] and the desired sum is 4. Should we output the pair (1, 3) twice or just once? Also do we output the reverse of a pair, meaning both (3, 1) and (1, 3)? Let’s keep the output as short as possible and print each pair only once. So, we will output only one copy of (1, 3). Also note that we shouldn’t output (2, 2) because it’s not a pair of two distinct elements.

## Example

```
f(10, [3, 4, 5, 6, 7]) // [ [6, 4], [7, 3] ]
f(10, [3, 4, 5, 6, 7]) // [ [4, 6], [3, 7] ]
f(8, [3, 4, 5, 4, 4]) // [ [3, 5], [4, 4], [4, 4], [4, 4] ]
f(10, [3, 5, 6, 8]) // []
```

## Source
Expand Down
2 changes: 1 addition & 1 deletion problems/balanced-brackets/Readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Balanced Brackets

Write a function that accepts a string consisting entiring of brackets (`[](){}`) and returns whether it is balanced. Every "opening" bracket must be followed by a closing bracket of the same type. There can also be nested brackets, which adhere to the same rule.
Write a function that accepts a string consisting entirely of brackets (`[](){}`) and returns whether it is balanced. Every "opening" bracket must be followed by a closing bracket of the same type. There can also be nested brackets, which adhere to the same rule.

```js
f('()[]{}(([])){[()][]}') // true
Expand Down
9 changes: 9 additions & 0 deletions problems/closest-sum/problem.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target.
Return the sum of the three integers.
You may assume that each input would have exactly one solution.

Example:
given array S = {-1 2 1 -4},
and target = 1.

The sum that is closest to the target is 2. (-1 + 2 + 1 = 2)
41 changes: 41 additions & 0 deletions problems/shop-in-candy-store/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Shop in Candy Store

In a candy store there are N different types of candies available and the prices of all the N different types of candies are provided to you.
You are now provided with an attractive offer.
You can buy a single candy from the store and get atmost K other candies ( all are different types ) for free.
Now you have to answer two questions. Firstly, you have to tell what is the minimum amount of money you have to spend to buy all the N different candies. Secondly, you have to tell what is the maximum amount of money you have to spend to buy all the N different candies.
In both the cases you must utilize the offer i.e. you buy one candy and get K other candies for free.


# Input
The first line of the input contains T the number of test cases. Each test case consists of two lines. The first line of each test case contains the values of N and K as described above. Then in the next line N integers follow denoting the price of each of the N different candies.


# Output
For each test case output a single line containing 2 space separated integers , the first denoting the minimum amount of money required to be spent and the second denoting the maximum amount of money to be spent.
Remember to output the answer of each test case in a new line.

# Constraints
1 <= T <= 50
1 <= N <= 1000
0 <= K <= N-1
1 <= Ai <= 100

# Example:
Input
1
4 2
3 2 1 4

Output
3 7

# Explanation
As according to the offer if you but one candy you can take atmost two more for free.
So in the first case you buy the candy which costs 1 and take candies worth 3 and 4 for free, also you buy candy worth 2 as well.
So min cost = 1+2 =3.
In the second case I buy the candy which costs 4 and take candies worth 1 and 2 for free, also I buy candy worth 3 as well.
So max cost = 3+4 =7.

# Topics :
Greedy || Sorting
31 changes: 31 additions & 0 deletions problems/skiing-in-singapore/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Skiing in Singapore
Sometimes it's nice to take a break and code up a solution to a small, fun problem. Here is one some of our engineers enjoyed recently called Skiing In Singapore.

Well you can’t really ski in Singapore. But let’s say you hopped on a flight to the Niseko ski resort in Japan. Being a software engineer you can’t help but value efficiency, so naturally you want to ski as long as possible and as fast as possible without having to ride back up on the ski lift. So you take a look at the map of the mountain and try to find the longest ski run down.

In digital form the map looks like the number grid below.

4 4
4 8 7 3
2 5 9 3
6 3 2 5
4 4 1 6

The first line (4 4) indicates that this is a 4x4 map. Each number represents the elevation of that area of the mountain. From each area (i.e. box) in the grid you can go north, south, east, west - but only if the elevation of the area you are going into is less than the one you are in. I.e. you can only ski downhill. You can start anywhere on the map and you are looking for a starting point with the longest possible path down as measured by the number of boxes you visit. And if there
are several paths down of the same length, you want to take the one with the steepest vertical drop, i.e. the largest difference between your starting elevation and your ending elevation.

On this particular map the longest path down is of length=5 and it’s highlighted in bold below: 9-5-3-2-1.

4 4
4 8 7 3
2 5 9 3
6 3 2 5
4 4 1 6

There is another path that is also length five: 8-5-3-2-1. However the tie is broken by the first path being steeper, dropping from 9 to 1, a drop of 8, rather than just 8 to 1, a drop of 7.

Your challenge is to write a program in your favorite programming language to find the longest (and then steepest) path on this map specified in the format above. It’s 1000x1000 in size, and all the numbers on it are between 0 and 1500.

Send your code or a github link (and a resume if you like) to [?????? at redmart dot com], replacing “??????” with the concatenation of the length of the longest path with the largest drop, and the size of the drop. So in the simple example above length=5, drop=8, so the email address would be [58 at redmart dot com]. If your e-mail gets through - you got the right answer.

Good luck and have fun!
27 changes: 27 additions & 0 deletions problems/spreadsheet/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Programming Challenge

A spreadsheet consists of a two-dimensional array of cells, labeled A1, A2, etc. Rows are identified using letters, columns by numbers. Each cell contains either an integer (its value) or an expression. Expressions contain integers, cell references, and the operators '+', '-', '\*', '/' with the usual rules of evaluation – note that the input is RPN and should be evaluated in stack order.

The spreadsheet input is defined as follows:

1. Line 1: two integers, defining the width and height of the spreadsheet (n, m)

2. n\*m lines each containing an expression which is the value of the corresponding cell (cells enumerated in the order A1, A2, A<n>, B1, ...)

## The Input
3 2
A2
4 5 *
A1
A1 B2 / 2 +
3
39 B1 B2 * /

## The Output
3 2
20.00000
20.00000
20.00000
8.66667
3.00000
1.50000
53 changes: 53 additions & 0 deletions problems/stack-machine/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Stack Machine

A stack machine is a simple system that performs arithmetic operations on an input string of numbers and operators. It contains a stack that can store an arbitrary number of 12-bit unsigned integers. Initially the stack is empty. The machine processes a string of characters in the following way:

- the characters of the string are processed one by one;
- if the current character is a digit `[0-9]`, the machine pushes the value of that digit onto its stack;
- if the current character is `+`, the machine pops the two topmost values from its stack, adds them and pushes the result onto the stack;
- if the current character is `*`, the machine pops the two topmost values from its stack, multiplies them and pushes the result onto the stack;
- after the machine has processed the whole string it returns the topmost value of its stack as the result;
- the machine reports an error if any operation it performs (addition or multiplication) results in an overflow;
- the machine reports an error if it tries to pop an element from its stack when the stack is empty, or if the stack is empty after the machine has processed the whole string.

For example, given the string "13+62*7+*" the machine will perform the following operations:
```
character | comment | stack
-----------------------------------------------
| | [empty]
'1' | push 1 onto the stack |
| | 1
'3' | push 3 onto the stack |
| | 1, 3
'+' | perform addition |
| | 4
'6' | push 6 onto the stack |
| | 4, 6
'2' | push 2 onto the stack |
| | 4, 6, 2
'*' | perform multiplication |
| | 4, 12
'7' | push 7 onto the stack |
| | 4, 12, 7
'+' | perform addition |
| | 4, 19
'*' | perform multiplication |
| | 76
```

The machine will return `76` as the result as it is the topmost element of its stack.

Write a function:
class Solution { public int solution(string S); }

that, given a string S consisting of N characters containing input for the stack machine, returns the result the machine would return if given this string. The function should return −1 if the machine would report an error when processing the string.

For example, given `String S = "13+62*7+*"` the function should return `76`, as explained in the example above. Given `String S = "11++"` the function should return `-1`.

Assume that:
- the length of S is within the range [0..200,000];
- string S consists only of the following characters: "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+" and/or "*".

In your solution, focus on **correctness**. The performance of your solution will not be the focus of the assessment.

**Solution:** https://github.com/AntonioRedondo/CodingInterviewProblems/blob/master/src/StackMachine.java
35 changes: 35 additions & 0 deletions problems/transitive-dependencies/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Transitive Dependencies

A class that calculates how dependencies propagate between things such as classes in a program. One of the insidious things about dependencies is that they are transitive—if A depends on B and B depends on C, then A also depends on C. So, this code that calculates the full set of dependencies for a group of items. Example class for test:

```php

<?php

require_once 'transitive-dependencies.php';

class Dependencies_Test {

public $dependencies;

public function __construct() {
$this->dependencies = new DependenciesList();
}

public function add_direct_test() {
$this->dependencies->add_direct('A', 'B,C');
$this->dependencies->add_direct('B', 'C,E');
$this->dependencies->add_direct('C', 'G');
$this->dependencies->add_direct('D', 'A,F');
$this->dependencies->add_direct('E', 'F');
$this->dependencies->add_direct('F', 'H');
}

}

$test = new Dependencies_Test();
$test->add_direct_test();
$resultado = $test->dependencies->dependencies_for("A");

//print dependencies array in screen
var_dump($resultado);
20 changes: 20 additions & 0 deletions solutions/c/find-missing-element.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <stdio.h>
#include <stdlib.h>
// The differnce in sum of two arrays will give us the desired value.
// The other approach could be to look up for element each time but that will increase the time complexity of the program.
int find_missing(int orig[],int On,int shuffled[])
{
int i,sum=0;
for(i=0;i<On-1;i++)
sum+=orig[i]-shuffled[i];
return sum+orig[On-1];
}

int main()
{
int arr[]={1,3,5,6,2,5,6,8,9,23,45,67,87};
int shuffled_arr[]={1,2,5,6,8,9,3,45,87,6,67,5};
int On=sizeof(arr) / sizeof(arr[1]);
printf("The missing value is:%d\n",find_missing(arr,On,shuffled_arr));
return 0;
}
13 changes: 13 additions & 0 deletions solutions/c/multiples-of-3-and-5.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <stdio.h>

int sum_of_multiples(int n)
{ int sum=0;
n=n-1;
sum=((n/3)*((n/3)*3 +3))/2 + ((n/5)*((n/5)*5 +5))/2 - ((n/15)*((n/15)*15 + 15))/2; // sum of airthmetic progressions & De-Morgan's Law
return sum;
}
int main()
{
printf("%d\n",sum_of_multiples(1000));
return 0;
}
Loading