R

E

A

C

T

O

xamples 

epeat

ode

pproach

ptimize

est

{Immutable BST}

Review

  • What does the term 'immutable' mean?

 

 

 

 

 

  • What does the term 'BST' mean?

Review

  • What does the term 'immutable' mean?

Immutable data cannot be changed once created, leading to much simpler application development, no defensive copying, and enabling advanced memoization and change detection techniques with simple logic.

                            -https://facebook.github.io/immutable-js/

  • What does the term 'BST' mean?
  • Node based structure
  • No more than 2 children nodes
  • Keys on left are < than node key, keys on right are > than node key

 

The Question

Given: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Return: Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat

  • Create an immutable BST class
  • Constructor should accept at least the value for the root node
  • Two instance methods:
    • insert - takes a numerical value and returns a new BST
    • contains - takes a numerical value and returns a Boolean based on whether the tree contains it

Example

What output would you expect?

Solution

How does the logic in line 5 work?

Solution

Solution

const bst = new ImmutableBST(5);
const bstMore = bst.insert(4).insert(3).insert(1).insert(10).insert(11).insert(15).insert(2).insert(100);

Trace

bst.contains(12);

Final Question

Why is this prompt part of the functional programming unit?

Great Resource!

Interviewer Tips

 

  • Can use constructor function or ES6 class syntax
  • Make sure they pick up on the 'at least the value' for the root node (help them brainstorm about what else might be good, but don't necessarily give it to them)
  • Ok to work on contains before insert
  • Ask them to think about big O
  • Extra credit: Remove -- best to write min and max utility functions, and 'size' becomes useful

 

Immutable BST

By mschreiber

Immutable BST

Technical interview problem on finding the character index for every word that begins with a given string.

  • 1,046