Accenture Previous Year Coding Questions with
Python Solutions
Rat Count House
Given r rats, each eats unit food. An array arr[] has food amounts in each house. Find the minimum
number of houses required to satisfy all rats. Return -1 if array is None/empty, return 0 if not
enough food.
def rat_count_house(r, unit, arr):
if arr is None or len(arr) == 0:
return -1
total_required = r * unit
current_sum = 0
for i, food in enumerate(arr):
current_sum += food
if current_sum >= total_required:
return i + 1
return 0
print(rat_count_house(7, 2, [2, 8, 3, 5, 7, 4, 1, 2]))
Password Validator
Check if a given string satisfies conditions: length >= 4, must contain a digit, must contain an
uppercase letter, no spaces or '/', and first character not a digit.
def is_valid_password(password):
if len(password) < 4:
return False
if password[0].isdigit():
return False
has_digit = False
has_upper = False
for ch in password:
if ch == ' ' or ch == '/':
return False
if ch.isdigit():
has_digit = True
if ch.isupper():
has_upper = True
return has_digit and has_upper
print(is_valid_password("Akhila1"))
print(is_valid_password("akhila"))
Sum of Numbers Divisible by X or Y
Given an array and two numbers X, Y, find sum of elements divisible by X or Y.
def sum_divisible(arr, x, y):
return sum(num for num in arr if num % x == 0 or num % y == 0)
print(sum_divisible([1,2,3,4,5,6,7,8,9,10], 2, 5))
Second Largest Element
Find the second largest distinct element in an array. Return None if not possible.
def second_largest(arr):
unique = list(set(arr))
if len(unique) < 2:
return None
unique.sort(reverse=True)
return unique[1]
print(second_largest([12, 35, 1, 10, 34, 1]))
String Reversal by Words
Reverse the order of words in a given sentence but keep the words themselves intact.
def reverse_words(sentence):
return ' '.join(sentence.split()[::-1])
print(reverse_words("Hello World from Accenture"))
Number Base Conversion
Convert a decimal number into another base (e.g. binary, hex).
def convert_base(num, base):
if num == 0:
return "0"
digits = []
while num > 0:
remainder = num % base
if remainder < 10:
digits.append(str(remainder))
else:
digits.append(chr(ord('A') + remainder - 10))
num //= base
return ''.join(reversed(digits))
print(convert_base(25, 2))
print(convert_base(255, 16))
Compare Two Strings Without strcmp
Compare two strings lexicographically without using built-in compare.
def compare_strings(s1, s2):
i = 0
while i < len(s1) and i < len(s2):
if s1[i] != s2[i]:
return 1 if s1[i] > s2[i] else -1
i += 1
if len(s1) == len(s2):
return 0
return 1 if len(s1) > len(s2) else -1
print(compare_strings("apple", "apricot"))
print(compare_strings("test", "test"))
Swap Two Variables Without Third Variable
Swap values of two integers without using a third variable.
a, b = 5, 10
print("Before:", a, b)
a = a ^ b
b = a ^ b
a = a ^ b
print("After:", a, b)
Method Overriding Example
Demonstrate method overriding in Python OOP.
class A:
def show(self):
print("From class A")
class B(A):
def show(self):
print("From class B")
obj = B()
obj.show()
Find Missing Number
Given numbers from 1 to n with one missing, find the missing number.
def find_missing(arr, n):
total = n * (n + 1) // 2
return total - sum(arr)
print(find_missing([1, 2, 4, 5, 6], 6))