๐Ÿ“˜ Day 2 โ€“ Teacher's Day Challenge | FizzBuzz Logic & Conditionals

TanishiTanishi
2 min read

๐Ÿ—“๏ธ Date: July 26, 2025
๐Ÿ”ข Problems Solved Today: 1
๐ŸŽฏ Focus Topic: Loops, Conditional Logic, Strings

๐Ÿ’ป Platform: Leetcode


๐Ÿ”— Table of Contents


๐Ÿงฉ Problem 1 โ€“ FizzBuzz

๐Ÿ”— LeetCode Link โ€“ FizzBuzz #412
๐Ÿ“š Difficulty: Easy
๐Ÿง  Concepts: Modulo, Conditional Logic, String Arrays


๐Ÿ“„ Problem Statement (Summary):
Given an integer n, return a string array (1-indexed) such that:

  • "FizzBuzz" if divisible by both 3 and 5

  • "Fizz" if divisible by 3

  • "Buzz" if divisible by 5

  • Else, the number itself as a string


๐Ÿ’ก Approach:

  • Initialize a vector of size n

  • Loop from 1 to n

  • Check divisibility by 3 and 5 using %

  • Assign appropriate string at index i-1 (1-indexed logic)


๐Ÿงช Code (C++):

class Solution {
public:
    vector<string> fizzBuzz(int n) {
        vector<string> result(n);
        for (int i = 1; i <= n; i++) {
            if (i % 3 == 0 && i % 5 == 0)
                result[i - 1] = "FizzBuzz";
            else if (i % 3 == 0)
                result[i - 1] = "Fizz";
            else if (i % 5 == 0)
                result[i - 1] = "Buzz";
            else
                result[i - 1] = to_string(i);
        }
        return result;
    }
};

๐Ÿ“ธ Submission Screenshot:


โœ… Key Takeaways:

  • Condition ordering matters: check for 3 & 5 first

  • % operator helps easily detect multiples

  • String conversion using to_string() for fallback case

  • Indexing starts from i - 1 because result is 0-indexed


๐Ÿ“ˆ Daily Summary

MetricValue
Problems Solved1
Topics CoveredModulo, Strings, Loops
Tools UsedC++
Next FocusString Manipulation, Sliding Window
Day2 / 30

๐Ÿท๏ธ Tags:
#leetcode #43DaysChallenge #fizzbuzz #cpp #strings #loops #conditionals #dsa

0
Subscribe to my newsletter

Read articles from Tanishi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Tanishi
Tanishi