Sorted set is a datatype which keeps a unique set of values in sorted order.
For example, this library can be used as follows:
var foo = new SortedSet([2, 4, 1]);
// Returns "1,2,4"
foo.toString();
foo.add(3);
// Returns "1,2,3,4"
foo.toString();
// Duplicate element has no effect
foo.add(3);
// Still returns "1,2,3,4"
foo.toString();
This is a skeleton code for frontend JavaScript takehome assignment which job candidates are expected to fork and submit their solutions.
Clone this repository and implement a sorted set.
These are the functions that need to be implemented for this assignment:
- add(el): Adds new element to the sorted set if not already in set
- clear(): Clears all elements in set
- contains(el): Returns
true
if a given element exists in the set - get(startIndex, endIndex): Gets elements between
startIndex
andendIndex
. IfendIndex
is omitted, a single element atstartIndex
is returned. - getBetween(lbound, ubound, exclusive): Gets all elements between
specified value range. If
exclusive
istrue
, values at lower bound and upper bound are not inclusive. - remove(element): Removes element from set and returns the element.
- removeAt(index): Removes element at index location and returns the element.
- removeBetween(lbound, ubound, exclusive): Removes elements between
specified value range. If
exclusive
istrue
, elements are remove exclusively.
You may use the following built-in JavaScript array methods for your implementation:
concat(a, b, c, ...)
: joins two or more arrays and returns a copy of the joined arraysindexOf(x)
: returns the index of an elementx
search from the start of the array or returns -1 if element cannot be foundlastIndexOf(x)
: returns the last index of an elementx
searching from the end of the array or returns -1 if element cannot be foundpop()
: removes the last array element and returns the elementpush(x)
: adds new elementx
to the end of the array and returns the new array lengthreverse()
: reverses the order of elementsshift()
: removes and returns the first array element (as opposed topop()
)slice(i, j)
: slices the elements between indexi
andj
and returns as a new arraysort()
: sorts the array elementssplice()
: adds/removes elements to/from the array and returns removed elementsunshift(x)
: adds new elementx
to the beginning of the array and returns the new array length
You are not allowed to use external JavaScript libraries. Do not worry about efficiency in sorting or runtime (e.g. O(n²) vs O(log n)) as we are trying to see if you can take the basic JavaScript building blocks and write a solution to solve a problem.
Results will be tested against the unit test that is included in the project.
Be sure to install Node.js, mocha.js before running tests.
You can run the tests with npm test
command.
The candidates will be graded on code quality, DRYness, and creative solutions.