Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Interview Quiz
- ## Quick Recap
- ### Big O
- Big O is the language and metric we use to describe the efficiency of algorithms. In theory, big O means the upper bound limit, but for practical purposes we'll use the tightest/closer upper bound possible (basically what academics call big theta). For example, iterating over all elements of an array should be described as O(n), although in theory O(n^2), O(n^3) are also valid upper limits.
- Remember that O(1) < O(log n) < O(n) < O(n * log n) < O(n^2).
- Also remember to drop constants, basically O(2 * n) could simply be described as O(n).
- **Time complexity** is the asymptotic runtime of an algorithm. It's how fast it runs. For example, iterating over an array is O(n).
- **Space complexity** is the asymptotic amount of memory used by an algorithm. It's how much memory it takes.
- ## Quiz 1 - Array
- Edit this markdown by replacing the `> EDIT ME`s below with your answers.
- ### Find key
- Given an array `A` of alpha characters `[A-Za-z]`, create a function that returns the first index at which a given element can be found in the array, or `-1` if it is not present.
- **Example of `A`**
- | i | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- |
- | A[i] | G | d | e | Z | e | k | I | X | d | S | e |
- **Solution**
- ```
- const findIndex = (array,self, cb) => {
- let len = array.length;
- let i;
- if (len === 0) return -1;
- if (typeof cb !== 'function') {
- throw new TypeError(cb);
- }
- if (self) {
- for (i = 0; i < len; i++) {
- if (cb.call(self, array[i], i, array)) {
- return i;
- }
- }
- } else {
- for (i = 0; i < len; i++) {
- if (cb(array[i], i, array)) {
- return i;
- }
- }
- }
- return -1;
- }
- let myArray = ["G","d","e","Z","e","k","I","X","d","S","e"];
- // so now find index of array has value is: "A"
- let findValue = "A";
- let findItem = findIndex(myArray, findValue, (item) => item === findValue );
- console.log(findItem);
- // for testing this code you can copy it and paste to : https://babeljs.io/repl/
- ```
- **Time and space complexities**
- > EDIT ME
- ### Are values unique?
- Create another function to identify whether `A` has unique elements.
- **Solution**
- ```
- let myArray = ["G","d","e","Z","e","k","I","X","d","S","e"];
- // check if "A" is unique element
- let findValue = "A";
- let findItem = myArray.filter((i) => i === findValue);
- if(findItem.length){
- if(findItem.length === 1){
- // A is unique
- console.log(findValue + ' is unique');
- }else{
- console.log(findValue + ' is duplicated');
- }
- }else{
- console.log("Not found");
- }
- ```
- **Time and space complexities**
- > EDIT ME
- **Extra**: What if you have memory/space limitations and you need to minimize the use of memory/data structures.
- **Solution**
- > EDIT ME
- **Time and space complexities**
- > EDIT ME
- ### Find key - extra
- What if `A` is known to be sorted using unique values, can you improve the performance to find the key?
- **Example of sorted and unique `A`**
- ```
- let myArray = ["G","G", "d","e","Z","e","k","I","X","d","S","e"];
- // let sort the arrays by items are duplicated. more duplicate items will be first of the array.
- let sortingArray = myArray.sort((a,b) => {
- let l1 = myArray.filter((a1) => a1 == a).length;
- let l2 = myArray.filter((b1) => b1 == b).length;
- return l2 - l1;
- })
- console.log(sortingArray);
- ```
- **Solution**
- > EDIT ME
- **Time and space complexities**
- > EDIT ME
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement