Data Structure and Algorithm Common Interview the Pyramid and Find the Vowels Challange.

CG_Musta
The Startup
Published in
5 min readNov 22, 2020

Please check part 3of this blog for more exercise.

In this 4th part of my weekly interview question blog, we going to explore 2 important questions: Pyramid and Find The Vowels.

Pyramid

This challenge is probably one of the most difficult ones to explain and solve if you never saw the solution before, I will do my best to explain the solution but it would probably take you some times to figure out the logic by yourself. However, if you followed part 3 of this series of blogs, the step question is similar to this one, and I suggest you take a look at it before to try solving the following challenge.

The Question:

Write a function that accepts a positive number N. The function should console log a pyramid shape with N levels using the # character. Make sure the pyramid has spaces on both the left *and* right-hand sides

Examples

pyramid(1)‘#’`pyramid(2)‘ # ‘

‘###’

pyramid(3)
‘ # ‘‘ ### ‘‘#####’

As you can see is similar to the step one in the previous blog but you need to create a pyramid shape in this one and where you don't have the # sign you need to add a space “ ”.

Solution 1

Example using an input of 3 in our function:

  1. The best way to visualize the solution is by drawing down what happens inside the output, as you can see we have some column and rows. The first things to find is the patter between N row and N of the column. The number of rows is pretty easy as it’s equal to the N number. For the number of columns, the relation is (N*2 -1), we need to double our input and subtract 1 and we get the number of columns.

2. We need to find the middle point and save it into a variable, and we can find the middle with the flowing calculation: Math.floor((2 * n -1)/2)

3. Create a for loop from 0 to N, to be able to loop through the rows.

4. Inside the first loop we create the variable solution or Pyramid whatever you prefer, and this is going to be the solution that we will console.log to the user.

5. now we need a second for loop, to loop through the number of columns.

6. Now we need to figure out which condition to write to make sure that we add a # or space ‘ ’ where we need. I will show you the code first and then with a table, I will try to explain the logic of the code.

for(let column = 0; column < 2 * n -1; column++){   if(midPoint - row <= column && midPoint + row >= column){      level += "#"    } else {     level += " "}

Here we are checking if our midpoint minus the current row is smaller or equal to the current column we are iterating && if the midpoint plus the current row is greater or equal to the current column.

Best way to visualize the solution here is with the current table I created which use the operator && to find where to place the #. Remember that both conditions need to be met to add the # else we are going to ad space “ ”.

Hopefully is not too confusing for you, but we are going to analyse together this table. First, we consider that our N number is 3 and our middle point is 2 on the first 2 columns you can see the condition we are looking for with our if statement and since the condition is 2 for each loop I created to a different row if both are met I designed a circle around, as you can see some are met and some are not but when both are met we place our # and if you take a close look to our circle where both conditions are met you can see a Pyramid shape, and if you compared to the first table that shows our output you can see are similar.

The Code:

Find The Vowels

Now is time to take a more easy challenge after the previous one.

The question:

Write a function that returns the number of vowels used in a string. Vowels are the characters ‘a’, ‘e’ ‘i’, ‘o’, and ‘u’.

Examples

vowels(‘Hi There!’) → 3vowels(‘Why do you ask?’) → 4vowels(‘Why?’) → 0

we need to consider also the lower and the upper case in this solution.

The Solution

  1. Create an array where to store all the Vowels we want to check in our string.
  2. Take our input string and transform it into an array that we can iterate.
  3. Create a variable that counts the number of Vowels we find inside our array.
  4. Use for loop to iterate into the array and for each element we need to lowercase it and check if is included in our array using the Includes method.
  5. If a vowel is found add 1 to our count otherwise don't do anything and return the count in the end.

The code

I hope you enjoyed these new challenges we analysed together in this blog, if you have a better solution, which I am sure exists, please comment below :).

--

--