My Docs
My BlogPython Data StructuresRepoFamily-Promise-Docs
Web-Dev-Hub-Docs
Web-Dev-Hub-Docs
  • Home
  • Navigation
  • Tools
    • Tools
      • Docker
      • G-Cloud & Firebase
      • Heroku
      • Dropbox
      • Email
      • Tools
      • DuckDuckGo
      • Elasticsearch
      • CodeSandbox
      • Product Hunt
      • Raycast
      • Elasticsearch
      • Tools
      • Showcase
        • Utilities
        • Continued
          • Page 3:
    • Downloads
    • REPL.IT Compilations
      • Part 2
    • Past Notes
      • Command Line Reference
    • Git
    • JavaScript
      • Interview Problems
      • General JavaScript Notes
      • jQuery
      • NodeJS
        • Node argv
        • NPM
        • Express
          • ExpressJS Overview
          • Sequelize
      • Regex
      • React
        • React Examples:
        • Redux
        • Redux Docs
          • Redux Resources
        • React Notes
    • My Bash Commands
    • Learning
  • Programming Languages
    • Programming Languages
      • File System
      • Basics
      • PSEUDO Programing Languages
        • HTML
      • CSS
      • List Of Programming Languages By Type
      • Tools-Of-The-Trade
        • Linux
        • Docker
      • Git
      • Python
        • Pydrive:
        • Practice
        • Pipenv
        • Untitled
      • Bash
        • SED
      • CHEATSHEETS
      • Java
      • Html
      • Markdown
      • CSS
      • SCSS
      • C & C++
      • Ruby
      • SQL
        • PostgreSQL
      • Jest
      • JavaScript
      • Typescript
      • C++
      • Babel
    • What is a Programming Language?
  • Python
    • Python
      • Python General Notes
      • Python Quiz
      • Python Cheat Sheet
      • Python Snippets
      • Python at length
    • Install PIP
  • JavaScript
    • JavaScript
      • Jquery
      • Page 16
    • Writing Files
    • JS-Leetcode
  • Web Development Frameworks & Libraries
    • GRAPHQL
    • React
    • Jquery
      • Prac
    • GATSBY
      • Untitled
      • Building with Components
      • Plugins, Themes, & Starters
      • GraphQL Concepts
  • Productivity
    • Productivity
      • Awesome Productivity Tools for Back-to-School
  • Misc
    • Misc
      • Experiments
  • GitGateway
    • Links
    • Bookmarks
  • Websites
    • Websites
    • Not My Websites:
    • Articles
  • Backend
    • Backend
  • Networking
    • Networks
  • Resources
    • Web Dev Tutorials
      • Node
        • API Security
    • Resources
    • Video Resources
  • General Knowledge
    • General Knowledge
    • Glossary
    • Knowledge Bank
  • Finance
    • Finance
    • Finance Reference
    • Financial Trends
  • Science & Tech (Innovation)
    • Science & Tech
    • Articles
  • Reading
    • Reading
  • Social Media & Trends
    • Trends In Web Dev
    • Analytics
    • FB-Dev-Open Source
      • Content Publishing
    • IG-API
  • Docs
    • Docs
      • NodeJS
        • installed it?
        • Timers in Node.js and beyond
        • Node.js web app
        • Overview of Blocking vs Non-Blocking
        • Don't Block the Event Loop (or the Worker Pool)
  • Code Editors & Tools
    • Vscode
      • Vscode Docs
      • How To Speed Up Vscode
  • Cool Stuff
    • Cool Observable Notebooks
  • Server-Side
    • GraphQL
      • Intro
    • Rest VS GraphQl
    • REST-API
    • Public APIs
  • WEB_DEV_TOOLS
    • Web Dev Tools
    • Cloudinary
    • Postman
      • Creating an API
      • Trouble Shooting Postman
    • Netlify
      • Facebook Graph API
      • Pandoc
      • Graph API
      • Troubleshooting
      • Examples
      • HTTPS (SSL)
      • Open Authoring
      • Netlify CMS
      • Git Gateway
  • DS_ALGOS_BRAINTEASERS
    • A Quick Guide to Big-O Notation, Memoization, Tabulation, and Sorting Algorithms by Example
  • Free-Stuff
    • Free Stuff
  • Job-Search
    • Job Search
    • Outreach
  • General Comp Sci
    • Principles behind the Agile Manifesto
  • Blockchain & Crypto
    • Blockchain Basics
      • Basics:
  • Data Structures & Interviewing
    • Data Structures
    • Computational Complexity
  • REACT_REVISITED
    • Modern React with Redux
      • React-Projects
  • WEBDEV-Bootcamp-Notes
    • 🏫Lambda
      • 1.1 - User Interface and Git
      • Page 2
      • Page 1
      • Page 3
      • Page 4
      • Page 5
      • Page 6
      • Page 7
      • Page 8
      • Page 9
      • Page 10
      • Page 11
      • Page 12
      • Page 13
      • Page 14
      • Page 15
      • CS-Python-Notes
        • Python
  • Unsorted-Notes
    • Compiled-Random-Notes
    • Testing:
      • Configure Jest for Testing JavaScript Applications
      • install, configure, and script Cypress for JavaScript web applications
      • Test React Components with Jest and `react-testing-library`
      • Use testing library to evaluate any framework...
  • Medium-articles
    • My Articles
      • Python For JS Developers
      • JavaScript Programmer
      • Awesome Web Development Youtube Video Archive
      • Bash Commands That Save Me Time and Frustration
      • Git-Tricks
      • scrap
      • Medium Article
      • Everything You Need To Know About Relational Databases, SQL, PostgreSQL and Sequelize To Build…
      • Machine Learner
      • Here’s the expanded list:
      • The Complete JavaScript Reference Guide
      • This is really cool!
      • Web Development Interview Part 3💻
      • Mutability And Reference VS Privative Types in JavaScript
      • React
      • Super Simple Intro To HTML
      • Introduction to React for Complete Beginners
      • Web Developer Resource List Part 2
      • Front End Interview Questions Part 2
      • A List Of Tools For Improvement
      • Github Repositories That Will Teach You How To Code For Free!
      • Libraries
      • Machine Learner
      • Here’s the expanded list:
      • The Complete JavaScript Reference Guide
  • 🖲️AI
    • Pytorch
      • Documentation
  • 🎙️Audio
    • Audio
Powered by GitBook
On this page
  • All:
  • W8:
  • W10:
  • GS Interview Questions:

Was this helpful?

  1. Tools

Past Notes

PreviousPart 2NextCommand Line Reference

Last updated 3 years ago

Was this helpful?

All:

W8:

w9:

W10:

📗

GS Interview Questions:

//Search Tree
/*
 Instructions to candidate.
  1) Run this code in the REPL to observe its behaviour. The
   execution entry point is main().
  2) Implement the "put" and "contains" methods.
  3) Fix the "inOrderTraversal" method.
  4) Add additional relevant tests
  5) If time permits, try to improve your implementation.
*/
dln;

let _ = require('underscore');

class Node {}

function put(node, value) {
    // TODO implement me
}

function contains(node, value) {
    // TODO implement me
}

function inOrderTraversal(node) {
    return inOrderTraversalAcc(node, []);
}

function inOrderTraversalAcc(node, acc) {
    if (!node.value) {
        return acc;
    }

    inOrderTraversalAcc(node.left, acc);
    acc.push(node.value);
    inOrderTraversalAcc(node.right, acc);
    return acc;
}

function assert(condition) {
    if (!condition) throw new Error();
}

function testTree() {
    let tree = new Node();
    put(tree, 3);
    put(tree, 1);
    put(tree, 2);
    put(tree, 5);
    assert(!contains(tree, 0));
    assert(contains(tree, 1));
    assert(contains(tree, 2));
    assert(contains(tree, 3));
    assert(!contains(tree, 4));
    assert(contains(tree, 5));
    assert(!contains(tree, 6));
    assert(_.isEqual(inOrderTraversal(tree), [1, 2, 3, 5]));
}

function doTestsPass() {
    testTree();
    // TODO add more tests
}

/**
 * Main execution entry.
 */
doTestsPass();
console.log('Success!');

let _ = require('underscore');

class Node {}

function put(node, value) {
    if (!value) {
        return;
    }

    if (!node.value) {
        node.value = value;
        node.left = new Node();
        node.right = new Node();
    } else {
        if (value < node.value) {
            put(node.left, value);
        } else {
            put(node.right, value);
        }
    }
}

function contains(node, value) {
    if (!node || !value) {
        return false;
    } else if (node.value === value) {
        return true;
    } else if (node.value && value < node.value) {
        return contains(node.left, value);
    } else {
        return contains(node.right, value);
    }
}

function inOrderTraversal(node) {
    return inOrderTraversalAcc(node, []);
}

function inOrderTraversalAcc(node, acc) {
    if (!node.value) {
        return acc;
    }

    inOrderTraversalAcc(node.left, acc);
    acc.push(node.value);
    inOrderTraversalAcc(node.right, acc);
    return acc;
}

function assert(condition) {
    if (!condition) {
        throw new Error();
    }
}

function testTree() {
    let tree = new Node();
    put(tree, 3);
    put(tree, 1);
    put(tree, 2);
    put(tree, 5);
    assert(!contains(tree, 0));
    assert(contains(tree, 1));
    assert(contains(tree, 2));
    assert(contains(tree, 3));
    assert(!contains(tree, 4));
    assert(contains(tree, 5));
    assert(!contains(tree, 6));
    assert(_.isEqual(inOrderTraversal(tree), [1, 2, 3, 5]));
}

function testEmptyTree() {
    let emptyTree = new Node();
    assert(_.isEqual(inOrderTraversal(emptyTree), []));
}

function testNegative() {
    let negativeTree = new Node();
    put(negativeTree, -1);
    put(negativeTree, 11);
    put(negativeTree, -10);
    put(negativeTree, 50);
    assert(contains(negativeTree, -10));
    assert(contains(negativeTree, -1));
    assert(contains(negativeTree, 11));
    assert(contains(negativeTree, 50));
    assert(_.isEqual(inOrderTraversal(negativeTree), [-10, -1, 11, 50]));
}

function testDupes() {
    let dupeTree = new Node();
    put(dupeTree, 1);
    put(dupeTree, 2);
    put(dupeTree, 1);
    put(dupeTree, 2);
    assert(contains(dupeTree, 1));
    assert(contains(dupeTree, 2));
    assert(_.isEqual(inOrderTraversal(dupeTree), [1, 1, 2, 2]));
}

function testUndefined() {
    let undefinedTree = new Node();
    put(undefinedTree, undefined);
    assert(!contains(undefinedTree, undefined));
    assert(_.isEqual(inOrderTraversal(undefinedTree), []));
}

function testNull() {
    let nullTree = new Node();
    put(nullTree, null);
    assert(!contains(nullTree, null));
    assert(_.isEqual(inOrderTraversal(nullTree), []));
}

function doTestsPass() {
    testTree();
    testEmptyTree();
    testNegative();
    testDupes();
    testUndefined();
    testNull();
}

/**
 * Main execution entry.
 */
doTestsPass();
console.log('Success!');
//Second Smallest
/**
 * Returns the second smallest element in the array x.
 * Returns 0 if the array has fewer than 2 elements.
 */
function secondSmallest(x) {
    // todo: implement this function
    return 0;
}

/**
 * Returns true if all tests pass; otherwise, returns false.
 */
function doTestsPass() {
    // todo: add more test cases
    let testArrays = [[0], [0, 1]];
    let testResults = [0, 1];

    // Run through the tests and make assertions
    for (let i = 0; i < testArrays.length; i++) {
        if (secondSmallest(testArrays[i]) != testResults[i]) {
            return false;
        }
    }
    return true;
}

/**
 * Main execution entry.
 */
if (doTestsPass()) {
    console.log('All tests pass!');
} else {
    console.log('There are test failures.');
}

function secondSmallest(x) {
    // First check if the array is large enough
    if (x.length < 2) {
        return 0;
    }

    // Start these at infinity so that they're always bigger
    // than the input
    let smallest = Number.POSITIVE_INFINITY;
    let secondSmallest = Number.POSITIVE_INFINITY;
    let current;

    // Loop through the input and keep updating our
    // smallest and second smallest
    for (let i = 0; i < x.length; i++) {
        current = x[i];
        if (current < smallest) {
            secondSmallest = smallest;
            smallest = current;
        } else if (current < secondSmallest) {
            secondSmallest = current;
        }
    }
    return secondSmallest;
}
dln;
/**
 * Returns true if all tests pass; otherwise, returns false.
 */
function doTestsPass() {
    // todo: add more test cases
    let testArrays = [[], [0], [0, 1], [-1, 0, 1, -2, 2], [1, 1, 2]];
    let testResults = [0, 0, 1, -1, 1];

    // Run through the tests and make assertions
    for (let i = 0; i < testArrays.length; i++) {
        if (secondSmallest(testArrays[i]) != testResults[i]) {
            return false;
        }
    }
    return true;
}

/**
 * Main execution entry.
 */
if (doTestsPass()) {
    console.log('All tests pass!');
} else {
    console.log('There are test failures.');
}