Friday, 30 May y
Accenture Interview Questions
1. Tell me about yourself ?
2. Explain a complex project you worked on. What were the challenges
and how did you overcome them?
3. Why do you want to work at Accenture, and how do you think you can
contribute to our team?
4. How do you stay updated with the latest technology trends and indus-
try developments?
5. Write a Python function to check if a given string is a palindrome.
6. How would you merge two dictionaries in Python?
7. How can you handle exceptions in Python? Provide an example of cus-
tom exception handling.
8. What is the purpose of __init__.py in Python packages?
9. What is the difference between a list and a tuple in Python?
10. What is the purpose of the self keyword in Python?
11. What is the difference between == and is in Python?
12. How do you convert a string to an integer in Python?
13. What is the difference between import and from…import?
Certainly! Here are some coding questions that you might en-
counter in Python interviews, along with brief explanations:
### Basic Coding Questions
1. **Reverse a String**
```python
def reverse_string(s: str) -> str:
return s[::-1]
```
2. **Find the Maximum Number in a List**
```python
def find_max(nums: list) -> int:
return max(nums)
```
3. **Check if a Number is Prime**
```python
def is_prime(n: int) -> bool:
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
1
Friday, 30 May y
if n % i == 0:
return False
return True
```
4. **Count Vowels in a String**
```python
def count_vowels(s: str) -> int:
vowels = 'aeiou'
return sum(1 for char in s.lower() if char in vowels)
```
5. **Find the Factorial of a Number**
```python
def factorial(n: int) -> int:
if n == 0:
return 1
return n * factorial(n - 1)
```
### Intermediate Coding Questions
6. **Fibonacci Sequence**
```python
def fibonacci(n: int) -> int:
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return a
```
7. **Check if a String is a Palindrome**
```python
def is_palindrome(s: str) -> bool:
return s == s[::-1]
```
8. **Merge Two Sorted Lists**
```python
def merge_sorted_lists(l1: list, l2: list) -> list:
return sorted(l1 + l2)
```
9. **Find the Intersection of Two Lists**
```python
def intersection(l1: list, l2: list) -> list:
return list(set(l1) & set(l2))
```
10. **Remove Duplicates from a List**
2
Friday, 30 May y
```python
def remove_duplicates(lst: list) -> list:
return list(set(lst))
```
### Advanced Coding Questions
11. **Find the Longest Substring Without Repeating Characters**
```python
def longest_unique_substring(s: str) -> str:
start, max_length, max_substr = 0, 0, ""
used_chars = {}
for end, char in enumerate(s):
if char in used_chars and used_chars[char] >= start:
start = used_chars[char] + 1
used_chars[char] = end
if end - start + 1 > max_length:
max_length = end - start + 1
max_substr = s[start:end + 1]
return max_substr
```
12. **Find the Kth Largest Element in an Array**
```python
import heapq
def kth_largest(nums: list, k: int) -> int:
return heapq.nlargest(k, nums)[-1]
```
13. **Rotate a Matrix 90 Degrees**
```python
def rotate_matrix(matrix: list) -> list:
return list(zip(*matrix[::-1]))
```
14. **Find All Anagrams of a Word in a List**
```python
from collections import Counter
def find_anagrams(word: str, words: list) -> list:
word_counter = Counter(word)
return [w for w in words if Counter(w) == word_counter]
```
15. **Implement a Basic LRU Cache**
```python
from collections import OrderedDict
class LRUCache:
def __init__(self, capacity: int):
self.cache = OrderedDict()
self.capacity = capacity
3
Friday, 30 May y
def get(self, key: int) -> int:
if key not in self.cache:
return -1
else:
self.cache.move_to_end(key)
return self.cache[key]
def put(self, key: int, value: int) -> None:
if key in self.cache:
self.cache.move_to_end(key)
self.cache[key] = value
if len(self.cache) > self.capacity:
self.cache.popitem(last=False)
```
### Data Structures
16. **Implement a Stack Using Queues**
```python
from collections import deque
class Stack:
def __init__(self):
self.queue1 = deque()
self.queue2 = deque()
def push(self, x: int) -> None:
self.queue1.append(x)
def pop(self) -> int:
while len(self.queue1) > 1:
self.queue2.append(self.queue1.popleft())
popped = self.queue1.popleft()
self.queue1, self.queue2 = self.queue2, self.queue1
return popped
def top(self) -> int:
return self.queue1[-1]
def empty(self) -> bool:
return not self.queue1
```
17. **Implement a Queue Using Stacks**
```python
class MyQueue:
def __init__(self):
self.stack1 = []
self.stack2 = []
4
Friday, 30 May y
def push(self, x: int) -> None:
self.stack1.append(x)
def pop(self) -> int:
self._move_elements()
return self.stack2.pop()
def peek(self) -> int:
self._move_elements()
return self.stack2[-1]
def empty(self) -> bool:
return not self.stack1 and not self.stack2
def _move_elements(self):
if not self.stack2:
while self.stack1:
self.stack2.append(self.stack1.pop())
```
18. **Find the Lowest Common Ancestor in a Binary Tree**
```python
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def lowest_common_ancestor(root: TreeNode, p: TreeNode, q:
TreeNode) -> TreeNode:
if not root:
return None
if root in (p, q):
return root
left = lowest_common_ancestor(root.left, p, q)
right = lowest_common_ancestor(root.right, p, q)
if left and right:
return root
return left or right
```
19. **Serialize and Deserialize a Binary Tree**
```python
import json
class Codec:
def serialize(self, root: TreeNode) -> str:
def encode(node):
if not node:
return None
5
Friday, 30 May y
return {'val': node.val, 'left': encode(node.left), 'right':
encode(node.right)}
return json.dumps(encode(root))
def deserialize(self, data: str) -> TreeNode:
def decode(data):
if data is None:
return None
node = TreeNode(data['val'])
node.left = decode(data['left'])
node.right = decode(data['right'])
return node
return decode(json.loads(data))
```
20. **Find the Longest Increasing Subsequence**
```python
def length_of_lis(nums: list) -> int:
if not nums:
return 0
dp = [1] * len(nums)
for i in range(1, len(nums)):
for j in range(i):
if nums[i] > nums[j]:
dp[i] = max(dp[i], dp[j] + 1)
return max(dp)
```
Pattern questions are a great way to test problem-solving skills
and understanding of loops and nested structures. Here are some
common pattern problems with their solutions in Python:
6
Friday, 30 May y
### 1. **Right-Angled Triangle**
Print a right-angled triangle pattern of numbers.
**Pattern:**
```
1
12
123
1234
```
**Code:**
```python
def right_angle_triangle(n: int):
for i in range(1, n + 1):
print(''.join(str(j) for j in range(1, i + 1)))
right_angle_triangle(4)
```
### 2. **Pyramid**
Print a pyramid pattern of stars.
**Pattern:**
```
*
***
*****
*******
```
**Code:**
```python
def pyramid(n: int):
for i in range(n):
print(' ' * (n - i - 1) + '*' * (2 * i + 1))
pyramid(4)
```
### 3. **Inverted Pyramid**
Print an inverted pyramid pattern of stars.
**Pattern:**
```
*******
*****
***
7
Friday, 30 May y
*
```
**Code:**
```python
def inverted_pyramid(n: int):
for i in range(n, 0, -1):
print(' ' * (n - i) + '*' * (2 * i - 1))
inverted_pyramid(4)
```
### 4. **Diamond**
Print a diamond pattern of stars.
**Pattern:**
```
*
***
*****
***
*
```
**Code:**
```python
def diamond(n: int):
for i in range(n):
print(' ' * (n - i - 1) + '*' * (2 * i + 1))
for i in range(n - 2, -1, -1):
print(' ' * (n - i - 1) + '*' * (2 * i + 1))
diamond(4)
```
### 5. **Floyd's Triangle**
Print Floyd's triangle.
**Pattern:**
```
1
23
456
7 8 9 10
```
**Code:**
```python
def floyds_triangle(n: int):
8
Friday, 30 May y
num = 1
for i in range(1, n + 1):
print(' '.join(str(num + j) for j in range(i)))
num += i
floyds_triangle(4)
```
### 6. **Pascal's Triangle**
Print Pascal's Triangle.
**Pattern:**
```
1
11
121
1331
```
**Code:**
```python
def pascals_triangle(n: int):
triangle = []
for i in range(n):
row = [1] * (i + 1)
for j in range(1, i):
row[j] = triangle[i - 1][j - 1] + triangle[i - 1][j]
triangle.append(row)
for row in triangle:
print(' ' * (n - len(row)) + ' '.join(map(str, row)))
pascals_triangle(4)
```
### 7. **Number Pyramid**
Print a number pyramid pattern.
**Pattern:**
```
1
121
12321
1234321
```
**Code:**
```python
def number_pyramid(n: int):
9
Friday, 30 May y
for i in range(1, n + 1):
print(' ' * (n - i) + ''.join(str(j) for j in range(1, i + 1)) +
''.join(str(j) for j in range(i - 1, 0, -1)))
number_pyramid(4)
```
### 8. **Hollow Square**
Print a hollow square pattern of stars.
**Pattern:**
```
*****
* *
* *
*****
```
**Code:**
```python
def hollow_square(n: int):
for i in range(n):
if i == 0 or i == n - 1:
print('*' * n)
else:
print('*' + ' ' * (n - 2) + '*')
hollow_square(5)
```
### 9. **Hollow Diamond**
Print a hollow diamond pattern.
**Pattern:**
```
*
**
* *
**
*
```
**Code:**
```python
def hollow_diamond(n: int):
for i in range(n):
if i < n // 2:
print(' ' * (n // 2 - i) + '*' + ' ' * (2 * i - 1) + ('*' if i > 0 else
''))
10
Friday, 30 May y
else:
print(' ' * (i - n // 2) + '*' + ' ' * (2 * (n - i - 1) - 1) + ('*' if i <
n - 1 else ''))
hollow_diamond(5)
```
### 10. **Checkerboard**
Print a checkerboard pattern.
**Pattern:**
```
****
***
****
***
```
**Code:**
```python
def checkerboard(n: int):
for i in range(n):
print(' '.join('*' if (i + j) % 2 == 0 else ' ' for j in range(n)))
checkerboard(4)
```
These pattern problems help in understanding the use of loops,
conditionals, and string manipulation in Python.
11