TATA Consultancy Limited aka TCS conducts a 120 hour program for new recruits called TCS Xplore. I see lot of engineering candidates are looking for Python Coding Questions asked in TCS.

I therefore decided to compile a few questions and answers that you can expect in TCS XPlore.

I encourage fellow engineers to contribute to questions and answers so that we all can benefit. I'll set the ball rolling.

## Q1. Write a Python function that takes a list of integers and returns the maximum product of any three integers in the list.

For example, if the input list is [-10, -10, 5, 2], the function should return 500 (which is the product of -10, -10, and 5).

Note: The list can have both negative and positive integers.

Answer -

To solve this problem, we need to find the maximum product of any three integers in the given list. We can do this by iterating through the list and keeping track of the maximum product of any three integers seen so far.

Algorithm:

- Sort the list in ascending order.
- Calculate the product of the last three elements in the sorted list.
- Calculate the product of the first two elements and the last element in the sorted list.
- Return the maximum of step 2 and step 3.

Here's the Python code that implements the above algorithm:

```
def max_product(nums):
nums.sort()
n = len(nums)
return max(nums[0] * nums[1] * nums[n-1], nums[n-3] * nums[n-2] * nums[n-1])
```

Let's break down the code and explain how it works:

`nums.sort()`

sorts the given list in ascending order.
`n = len(nums)`

stores the length of the list in a variable n.
`nums[0] * nums[1] * nums[n-1]`

calculates the product of the first two elements and the last element in the sorted list.
`nums[n-3] * nums[n-2] * nums[n-1]`

calculates the product of the last three elements in the sorted list.
`max()`

returns the maximum of the two products calculated in the previous steps.

Running the tests with some sample inputs -

```
print(max_product([-10, -10, 5, 2])) # Output: 500
print(max_product([-10, -10, -5, 2])) # Output: 200
print(max_product([1, 2, 3, 4, 5])) # Output: 60
```

In the first test case, the function correctly returns the maximum product of -10, -10, and 5, which is 500.

In the second test case, the function returns the maximum product of -10, -10, and 2, which is 200.

In the third test case, the function returns the maximum product of 3, 4, and 5, which is 60.

I hope you find this useful. Will add more questions soon.