Exercise Thursday

Whiteboard Practice

When and how whiteboarding practice is implemented will be up to your teacher. Below is a recommended prompt.

Goal

For this week’s whiteboarding lesson, we’ll focus on using arrays and loops to assess a string.

As the interviewee:

  • Ask clarifying questions.
  • Keep talking.
  • Explain your plan at the beginning; recap what you’ve done at the end.
  • Make eye contact.
  • Plan your space.

As the interviewer:

  • Answer questions as best as you can.
  • Be encouraging. Whiteboarding is difficult!
  • Be patient. Only offer hints if your partner indicates that they need help.
  • Be engaged. Part of this practice is getting used to having someone evaluate your work as your produce it.
  • Offer constructive feedback. Find at least one thing that your partner did well and one thing they could improve at.

Problem

Prompt: Write a method that takes in a string and counts each instance of each letter in the string. It should return an array of integers, where each integer represents the number of occurrences of a letter. The array should start with the number of as, and end with number of zs (i.e. if the array is called letters, then letters[0] should represent the number of as, and letters[25] should represent the number of zs).

Example:

  • Given: "As you know, Morty, I've got a lot of enemies in the universe that consider my genius a threat"
  • Return: [ 5, 0, 1, 1, 10, 1, 2, 3, 6, 0, 1, 1, 3, 6, 7, 0, 0, 4, 5, 8, 3, 2, 0, 0, 3, 0 ]

Further Exploration

  • Alter your method to return a variable that contains key-value pairs, where each key is a letter in the string, and the value is the number of times that letter occurred in the string. You might choose to dynamically create a variable name. For example, if you wanted to set "a" as a key and give a value 1, that would be written as follows: var letter = "a"; var letters={}; letters[letter] = 1;. Example: Return: { a:5, c:1, d:1, e:10, f:1, g:2, h:3, i:6, k:1, l:1, m:3, n:6, o:7, p:0, r:4, s:5, t:8, u:3, v:2, y:3 }

  • Write another method that returns true if the string is a pangram and false if it is not a pangram. A pangram is a sentence that contains at least one instance of each letter of the alphabet.

    • Alter this method so that it can parse a string that contains multiple sentences. The method should return false if no pangrams are present and should return an array of the sentences that are pangrams if there are any present. It is recommended that you create another helper method.