Data Structure and Algorithm Common Interview Questions (Part 3)

Please check part 2 of this blog for more exercise.

In this 3rd part of my weekly interview question blog, we going to explore 2 important questions: Capitalization and Print Steps.

Capitalization

Write a function that accepts a string. The function should capitalize the first letter of each word in the string then return the capitalized string.

Examples

 capitalize(‘a short sentence’) → ‘A Short Sentence’ capitalize(‘a lazy fox’) → ‘A Lazy Fox’ capitalize(‘look, it is working!’) → ‘Look, It Is Working!’

This question is not particularly challenging and has many different solutions, I will show you one of the easiest and more logical ways to approach this question during an interview.

Solution 1

  1. Createa another empty array.
  2. Loop inside each word and take the first character and use toUppercase method().
  3. Now you have 1 letter capitalized add the rest of the letter by using the slice 1 to exclude the 1st letter you alredy have and push it into the new array.
  4. Re-join the array and return the result

The Code:

In this solution, we have created a new array and push inside the capitalized word every loop. However, we could use the same array created without a new one by using a traditional for loop and shift our word manipulate it and push it back in the same array, example:

In this case, we are using the same array and we don’t need to create a new one, both solutions have the same logic and outcome, but you can choose the one that you understand better.

Print Steps

The question:

Write a function that accepts a positive number N. The function should console log a step shape with N levels using the # character. Make sure the step has spaces on the right-hand side!

Examples

 steps(2) ‘# ‘ ‘##’ steps(3) ‘# ‘ ‘## ‘ ‘###’ steps(4)‘# ‘‘## ‘‘### ‘‘####’

The challenging part here is not only printing the hash # but on the right side need to fill the spaces where the hash is not present with empty space.

To explain the logic of this question we need to draw a table:

This is the best way to understand what is going inside the logic of this output. In this example, we are using an input of 3. First, we can notice that the output looks like a grill, where we have column and rows. we have a row index which goes from 0 to 2 and same of the column index.

Take some time to look at this grill and see if you can find the pattern we are looking for to solve the challenge…

Solution 1.

  1. Loop from 0 to n representing our row
  2. create a variable equal to an empty string
  3. Create a nested loop from 0 to n representing our column
  4. If our current column is less or equal to the row add # else add space
  5. console log the result string inside the first loop.

The code:

Recursive solution :

first, let see some recursive tips to follow whenever trying to solve some challenges with this technique.

  1. Identify the bare minimum pieces of information to represent your problem
  2. Give reasonable default value to these pieces of information
  3. Check the base case if you do not have any work left to do return
  4. After writing some code, call your function again and make sure that some of the argument has changed to dont fall into an infite loop.

The solution:

  1. Create a function that takes an N number a integer row and a string result.
  2. Default value for row is zero and for result is empty string
  3. first check if row is equal to N in this case return the function.
  4. Second check if the result is equal to N in this case we done one entire row and we can console.log result and pass re-call the function with same n and row+1.
  5. If none of the above condition is met, we check if the length of the result string is less or equl to the current row, if yes we add # if is not we add space. we re-call the function by passing the current row current result and n.

The code:

This second solution is not easy to explain and to understand at first, is probably better if you copy the code and go through the logic slowly until you understand what is going on inside this function.

Follow me for more exercise, soon part 4.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store