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.
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.
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.
- Convert the string into an array to be able to loop into it.
- Createa another empty array.
- Loop inside each word and take the first character and use toUppercase method().
- 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.
- Re-join the array and return the result
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.
This is probably the first more challenging question so far in this series of blogs.
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!
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…
- Loop from 0 to n representing our row
- create a variable equal to an empty string
- Create a nested loop from 0 to n representing our column
- If our current column is less or equal to the row add # else add space
- console log the result string inside the first loop.
Recursive solution :
first, let see some recursive tips to follow whenever trying to solve some challenges with this technique.
- Identify the bare minimum pieces of information to represent your problem
- Give reasonable default value to these pieces of information
- Check the base case if you do not have any work left to do return
- 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.
- Create a function that takes an N number a integer row and a string result.
- Default value for row is zero and for result is empty string
- first check if row is equal to N in this case return the function.
- 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.
- 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.
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.