Skip to content

Instantly share code, notes, and snippets.

@jendiamond
Last active February 21, 2019 23:27
Show Gist options
  • Save jendiamond/c98add859519e1bbc6eafa2fff028a95 to your computer and use it in GitHub Desktop.
Save jendiamond/c98add859519e1bbc6eafa2fff028a95 to your computer and use it in GitHub Desktop.

A recipe for solving problems ⚡️⚡️

https://learn.makerpass.com/groups/ssp7w/courses/reactorcore/ssp-7w?id=module3%2Fhow-to-run-your-interview https://gist.github.com/jendiamond/a4254b4bffb479b27fbe99d0ac96ac86

Really read the problem

Read it slowly.
Read it again.

Write a function called “getElementsLessThan100AtProperty”. Given an object and a key, “getElementsLessThan100AtProperty” returns an array containing all the elements of the array located at the given key that are less than 100.

Identify input and outputs

  • Input = Object with a key whose value is an Array
  • Output = Array containing all the elements less than 100

Include the type of input / output (ie: Array String)
Include an explanation of what it is (ie: Array of matching elements from both Arrays that are over 10)

Declare the assumptions

  • Ask yourself, out loud what you are assuming

  • Ask at least one question about those assumptions

  • Include these assumptions in the tests

    • input is an Object
    • the value at the given key is an Array
    • the Array is not empty
    • that key exists in the Object

Possible questions that come to my mind:

  • How should I handle the 1st word being longer than the 2nd?
  • How should I handle the 2nd word being longer than the 1st?
  • Do I need to do anything about uppercase/lowercase?
  • Can there be spaces inside the string? If so, do I treat them as letters?

Tests

Create Tests and Checks from the assumptions

Declare the Assertions

  • "should return an array containing all the elements less than 100 in the array located at key"
  • "should return an empty array if the array has no elements less than 100"
  • "should return an empty array if the property is not an array"
  • "should return an empty array if the key does not exist"

Checks

if (obj instanceof Object && obj[key] instanceof Array && obj[key] != []) {};

Outline your approach in pseudo-code

Write out your approach line by line

// iterate though the array
// find the elements that are less than 10
// push those element to a new array
// output the elements that are less than 10
  • Decide on your core data structure or data type
  • What are the start and end states of that data?
  • How does that data "mutate" from start state to end state?
  • Outline your state-changing logic in English pseudo-code
  • Re-read it, edit it, make it clear and precise

Outline your structure with one or more empty functions (aka stub functions)

  • Give those stubs clear names
  • Each function -- has a clear contract
  • Write an assertion
  • Assert what behavior should be true about that component

Implement carefully, step-by-step

  • Fill in the main function body.
  • Stub out helper functions if need be.
  • Each function should ideally have a single purpose. It should do only one, small thing.
    That one thing is expressed clearly by the function's name.

Test as you go

  • Each function -- has a clear contract
  • Write an assertion
  • Assert what behavior should be true about that component
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment