Print Rhombus in JavaScript (Time Complexity: O(n))


Given an odd positive integer n print a rhombus of stars as in the example below

Example:

Input: n = 5
Output:
  *  
 *** 
*****
 *** 
  *  

Explanation:
There are n = 5 lines. Each line contains exactly n = 5 characters.
1st line contains two spaces, one '*' and two spaces.
2nd line contains one space, three '*' and one space.
3rd line contains five stars.
4th line contains one space, three '*' and one space.
5th line contains two spaces, one '*' and two spaces.
Note: Be careful to add the necessary spaces at the end of every line!


Understanding the Problem

The core challenge of this problem is to print a rhombus shape using stars ('*') and spaces (' '). The rhombus should be symmetric and centered, with the number of lines equal to the given odd integer n. This problem is significant in understanding how to manipulate strings and use loops effectively to create patterns.

Approach

To solve this problem, we need to understand the pattern of spaces and stars for each line:

Naive Solution

A naive solution would involve manually calculating the number of spaces and stars for each line and printing them. However, this approach is not optimal as it involves repetitive calculations and is not scalable.

Optimized Solution

An optimized solution involves using loops to calculate the number of spaces and stars for each line dynamically. This approach is more efficient and scalable.

Algorithm

Here is a step-by-step breakdown of the algorithm:

  1. Initialize a loop to iterate from 0 to n-1 (inclusive).
  2. For each iteration, calculate the number of spaces and stars based on the current line number.
  3. Print the line with the calculated number of spaces and stars.

Code Implementation

function printRhombus(n) {
  // Loop through each line
  for (let i = 0; i < n; i++) {
    // Calculate the number of stars and spaces
    let stars = i <= Math.floor(n / 2) ? 2 * i + 1 : 2 * (n - i - 1) + 1;
    let spaces = (n - stars) / 2;
    
    // Create the line with spaces and stars
    let line = ' '.repeat(spaces) + '*'.repeat(stars) + ' '.repeat(spaces);
    
    // Print the line
    console.log(line);
  }
}

// Example usage
printRhombus(5);

Complexity Analysis

The time complexity of this solution is O(n) because we are iterating through each line once. The space complexity is O(1) as we are using a constant amount of extra space.

Edge Cases

Potential edge cases include:

For n = 1, the output should be a single '*'. For large values of n, the algorithm should still perform efficiently.

Testing

To test the solution comprehensively, consider the following test cases:

Use a testing framework like Jest or simply run the function with different values of n to verify the output.

Thinking and Problem-Solving Tips

When approaching such problems, consider the following tips:

Conclusion

In this blog post, we discussed how to print a rhombus of stars given an odd positive integer n. We explored the problem definition, approach, algorithm, code implementation, complexity analysis, edge cases, and testing. Understanding and solving such problems helps improve problem-solving skills and coding proficiency.

Additional Resources

For further reading and practice, consider the following resources: