From 52970ad9c673a9452196e8c9649e72bbae83699a Mon Sep 17 00:00:00 2001 From: ivan Date: Sun, 12 Apr 2026 04:41:37 -0600 Subject: [PATCH] adding factorial trailing zeroes --- .../common_algos/two_sum_round_7.py | 21 ++++++++++ .../common_algos/valid_palindrome_round_7.py | 22 ++++++++++ .../ex_128_factorial_trailing_zeroes.py | 20 ++++++++++ .../ex_128_factorial_trailing_zeroes.ts | 17 ++++++++ ..._128_factorial_trailing_zeroes_round_22.py | 40 +++++++++++++++++++ 5 files changed, 120 insertions(+) create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_7.py create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_128_factorial_trailing_zeroes.py create mode 100644 src/my_project/interviews_typescript/top_150_questions_round_1/ex_128_factorial_trailing_zeroes.ts create mode 100644 tests/test_150_questions_round_22/test_128_factorial_trailing_zeroes_round_22.py diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.py new file mode 100644 index 00000000..c7f907c0 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_7.py @@ -0,0 +1,21 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + + answer = dict() + + for k, v in enumerate(nums): + + if v in answer: + return [answer[v], k] + else: + answer[target - v] = k + + return [] + + + + + diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_7.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_7.py new file mode 100644 index 00000000..fa21021b --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_7.py @@ -0,0 +1,22 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod +import re + +class Solution: + def isPalindrome(self, s: str) -> bool: + + # To lowercase + s = s.lower() + + # Remove non-alphanumeric characters + s = re.sub(pattern=r'[^a-zA-Z0-9]', repl='', string=s) + + # Determine if s is palindrome or not + len_s = len(s) + + for i in range(len_s//2): + + if s[i] != s[len_s - 1 - i]: + return False + + return True diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_128_factorial_trailing_zeroes.py b/src/my_project/interviews/top_150_questions_round_22/ex_128_factorial_trailing_zeroes.py new file mode 100644 index 00000000..24ab90a4 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_128_factorial_trailing_zeroes.py @@ -0,0 +1,20 @@ +class Solution: + def trailingZeroes(self, n: int) -> int: + """ + Count trailing zeroes in n! by counting factors of 5. + + Key insight: Trailing zeroes come from 10 = 2 * 5. + Since there are always more factors of 2 than 5 in n!, + we just need to count factors of 5. + + We count: floor(n/5) + floor(n/25) + floor(n/125) + ... + This counts all multiples of 5, 25, 125, etc. + + Time complexity: O(log n) - we divide by 5 each iteration + Space complexity: O(1) + """ + count = 0 + while n > 0: + n //= 5 + count += n + return count \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_128_factorial_trailing_zeroes.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_128_factorial_trailing_zeroes.ts new file mode 100644 index 00000000..81f5aec3 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_128_factorial_trailing_zeroes.ts @@ -0,0 +1,17 @@ +function trailingZeroes(n: number): number { + // Count trailing zeroes in n! by counting factors of 5 + // Trailing zeroes come from 10 = 2 * 5 + // Since there are always more factors of 2 than 5 in n!, + // we just need to count factors of 5 + // + // We count: floor(n/5) + floor(n/25) + floor(n/125) + ... + // Time: O(log n), Space: O(1) + + let count = 0; + while (n > 0) { + n = Math.floor(n / 5); + count += n; + } + return count; +}; + diff --git a/tests/test_150_questions_round_22/test_128_factorial_trailing_zeroes_round_22.py b/tests/test_150_questions_round_22/test_128_factorial_trailing_zeroes_round_22.py new file mode 100644 index 00000000..7beeb991 --- /dev/null +++ b/tests/test_150_questions_round_22/test_128_factorial_trailing_zeroes_round_22.py @@ -0,0 +1,40 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_128_factorial_trailing_zeroes import Solution + +class FactorialTrailingZeroesTestCase(unittest.TestCase): + def setUp(self): + self.solution = Solution() + + def test_example_1(self): + # 3! = 6, no trailing zero + self.assertEqual(self.solution.trailingZeroes(3), 0) + + def test_example_2(self): + # 5! = 120, one trailing zero + self.assertEqual(self.solution.trailingZeroes(5), 1) + + def test_example_3(self): + # 0! = 1, no trailing zero + self.assertEqual(self.solution.trailingZeroes(0), 0) + + def test_larger_number(self): + # 10! = 3628800, two trailing zeroes + self.assertEqual(self.solution.trailingZeroes(10), 2) + + def test_multiple_of_25(self): + # 25! has floor(25/5) + floor(25/25) = 5 + 1 = 6 trailing zeroes + self.assertEqual(self.solution.trailingZeroes(25), 6) + + def test_large_number(self): + # 100! has many trailing zeroes + # floor(100/5) + floor(100/25) + floor(100/125) = 20 + 4 + 0 = 24 + self.assertEqual(self.solution.trailingZeroes(100), 24) + + def test_single_digit(self): + # 4! = 24, no trailing zero + self.assertEqual(self.solution.trailingZeroes(4), 0) + + +if __name__ == "__main__": + unittest.main()