0% found this document useful (0 votes)
3 views8 pages

Interview Questions

The document provides solutions to 15 programming problems without using built-in functions, including FizzBuzz, Fibonacci sequence, palindrome check, and Armstrong numbers. Each problem is accompanied by a Python function that implements the solution using basic control structures and mathematical operations. Additionally, it includes examples and explanations for finding Armstrong numbers within a specified range.

Uploaded by

reshseene22
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views8 pages

Interview Questions

The document provides solutions to 15 programming problems without using built-in functions, including FizzBuzz, Fibonacci sequence, palindrome check, and Armstrong numbers. Each problem is accompanied by a Python function that implements the solution using basic control structures and mathematical operations. Additionally, it includes examples and explanations for finding Armstrong numbers within a specified range.

Uploaded by

reshseene22
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Here are the same 15 problems without using built-in functions:

1. FizzBuzz
def fizzbuzz(n):
i = 1
while i <= n:
if i % 3 == 0 and i % 5 == 0:
print("FizzBuzz")
elif i % 3 == 0:
print("Fizz")
elif i % 5 == 0:
print("Buzz")
else:
print(i)
i += 1

fizzbuzz(15)

2. Fibonacci Sequence
def fibonacci(n):
if n <= 1:
return n
a, b = 0, 1
count = 2
while count <= n:
temp = a + b
a = b
b = temp
count += 1
return b

print(fibonacci(10)) # Output: 55

3. Odd or Even
def odd_or_even(num):
if num % 2 == 0:
return "Even"
else:
return "Odd"

print(odd_or_even(7)) # Output: Odd

4. Palindrome Check
def is_palindrome(s):
# Convert to lowercase manually
lower_s = ""
for char in s:
if 'A' <= char <= 'Z':
lower_s += chr(ord(char) + 32)
elif char != ' ':
lower_s += char

# Check palindrome
left = 0
right = len(lower_s) - 1
while left < right:
if lower_s[left] != lower_s[right]:
return False
left += 1
right -= 1
return True

print(is_palindrome("Racecar")) # Output: True

5. Reverse a String
def reverse_string(s):
result = ""
i = len(s) - 1
while i >= 0:
result += s[i]
i -= 1
return result

print(reverse_string("hello")) # Output: olleh

6. Find Maximum in List


def find_max(lst):
if len(lst) == 0:
return None
max_val = lst[0]
i = 1
while i < len(lst):
if lst[i] > max_val:
max_val = lst[i]
i += 1
return max_val

print(find_max([3, 7, 2, 9, 1])) # Output: 9

7. Count Vowels
def count_vowels(s):
vowels = "aeiouAEIOU"
count = 0
for char in s:
for vowel in vowels:
if char == vowel:
count += 1
break
return count

print(count_vowels("Hello World")) # Output: 3

8. Factorial
def factorial(n):
if n == 0 or n == 1:
return 1
result = 1
i = 2
while i <= n:
result *= i
i += 1
return result

print(factorial(5)) # Output: 120


9. Prime Number Check
def is_prime(n):
if n < 2:
return False
i = 2
while i * i <= n:
if n % i == 0:
return False
i += 1
return True

print(is_prime(17)) # Output: True

10. Remove Duplicates from List


def remove_duplicates(lst):
result = []
for item in lst:
found = False
for existing in result:
if item == existing:
found = True
break
if not found:
[Link](item)
return result

print(remove_duplicates([1, 2, 2, 3, 4, 4, 5])) # Output: [1, 2, 3, 4, 5]

11. Sum of Digits


def sum_of_digits(n):
if n < 0:
n = -n
total = 0
while n > 0:
total += n % 10
n //= 10
return total

print(sum_of_digits(1234)) # Output: 10

12. Anagram Check


def is_anagram(s1, s2):
# Convert to lowercase
lower1 = ""
lower2 = ""

for char in s1:


if 'A' <= char <= 'Z':
lower1 += chr(ord(char) + 32)
else:
lower1 += char

for char in s2:


if 'A' <= char <= 'Z':
lower2 += chr(ord(char) + 32)
else:
lower2 += char
# Check lengths
if len(lower1) != len(lower2):
return False

# Count characters manually


for char in lower1:
count1 = 0
count2 = 0
for c in lower1:
if c == char:
count1 += 1
for c in lower2:
if c == char:
count2 += 1
if count1 != count2:
return False

return True

print(is_anagram("listen", "silent")) # Output: True

13. Find Second Largest Number


def second_largest(lst):
if len(lst) < 2:
return None

# Find largest
largest = lst[0]
for num in lst:
if num > largest:
largest = num

# Find second largest


second = None
for num in lst:
if num < largest:
if second is None or num > second:
second = num

return second

print(second_largest([10, 5, 8, 12, 3])) # Output: 10

14. Count Word Frequency


def word_frequency(s):
# Convert to lowercase
lower_s = ""
for char in s:
if 'A' <= char <= 'Z':
lower_s += chr(ord(char) + 32)
else:
lower_s += char

# Split into words


words = []
word = ""
for char in lower_s:
if char == ' ':
if word != "":
[Link](word)
word = ""
else:
word += char
if word != "":
[Link](word)

# Count frequency
freq = {}
for word in words:
if word in freq:
freq[word] += 1
else:
freq[word] = 1

return freq

print(word_frequency("hello world hello")) # Output: {'hello': 2, 'world':


1}

15. List Intersection


def list_intersection(lst1, lst2):
result = []
for item in lst1:
found_in_lst2 = False
for item2 in lst2:
if item == item2:
found_in_lst2 = True
break

if found_in_lst2:
# Check if already in result
already_added = False
for r in result:
if r == item:
already_added = True
break
if not already_added:
[Link](item)

return result

print(list_intersection([1, 2, 3, 4], [3, 4, 5, 6])) # Output: [3, 4]


Here’s the Armstrong Number problem without built-in functions:

## Armstrong Number (Narcissistic Number)

An Armstrong number is a number that is equal to the sum of its own digits each raised to the
power of the number of digits.

- 153 = 1³ + 5³ + 3³ = 1 + 125 + 27 = 153 ✓


**Examples:**

- 9474 = 9⁴ + 4⁴ + 7⁴ + 4⁴ = 6561 + 256 + 2401 + 256 = 9474 ✓


- 123 ≠ 1³ + 2³ + 3³ = 1 + 8 + 27 = 36 ✗
```python
Def power(base, exp):
“””Calculate base raised to exp without using **”””
Result = 1
I=0
While i < exp:
Result *= base
I += 1
Return result

Def count_digits(n):
“””Count number of digits without using len() or str()”””
If n == 0:
Return 1
Count = 0
Temp = n
While temp > 0:
Count += 1
Temp //= 10
Return count

Def is_armstrong(n):
“””Check if a number is an Armstrong number”””
Num_digits = count_digits(n)
Temp = n
Total = 0

While temp > 0:


Digit = temp % 10
Total += power(digit, num_digits)
Temp //= 10

Return total == n

# Test cases
Print(is_armstrong(153)) # Output: True
Print(is_armstrong(9474)) # Output: True
Print(is_armstrong(123)) # Output: False
Print(is_armstrong(370)) # Output: True
Print(is_armstrong(1)) # Output: True
Print(is_armstrong(0)) # Output: True
```
## Find All Armstrong Numbers in a Range

```python
Def find_armstrong_numbers(start, end):
“””Find all Armstrong numbers in a given range”””
Armstrong_list = []

Current = start
While current <= end:
If is_armstrong(current):
Armstrong_list.append(current)
Current += 1

Return armstrong_list

# Find all Armstrong numbers between 1 and 10000


Result = find_armstrong_numbers(1, 10000)
Print(result)
# Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474]
```

## Alternative: Print Armstrong Numbers

```python
Def print_armstrong_in_range(start, end):
“””Print all Armstrong numbers in range”””
Current = start
While current <= end:
Num_digits = count_digits(current)
Temp = current
Total = 0

While temp > 0:


Digit = temp % 10
Total += power(digit, num_digits)
Temp //= 10

If total == current:
Print(current)

Current += 1
Print(“Armstrong numbers between 1 and 1000:”)
Print_armstrong_in_range(1, 1000)
```

**Key Points:**
- We manually count digits by dividing by 10 repeatedly
- We implement our own power function using loops
- We extract each digit using modulo (%) and integer division (//)
- No use of `str()`, `len()`, `**` operator, or other built-ins

This is a common interview question that tests your understanding of loops, mathematical
operations, and problem-solving!

You might also like