Coding_Test_Solution
Coding_Test_Solution
Please Note: The solution for Coding Test is provided here in C, C++, Java, Python language.
CODING TEST
IT-1
(1) Maximum Depth of Binary Tree
Example 1:
Input: root = [3,9,20,null,null,15,7] Output:
3
Example 2:
Input: root = [1,null,2]
Output: 2
Constraints:
The number of nodes in the tree is in the range [0, 104].
-100 <= Node.val <= 100
Solution:
C:
Prepared by Kashish Zaveri, Samarth Joshi, Tarun Ray, Unnati Suryawanshi (T&P Coordinators) 21/03/2025
C++:
Java:
Python:
Prepared by Kashish Zaveri, Samarth Joshi, Tarun Ray, Unnati Suryawanshi (T&P Coordinators) 21/03/2025
(2) Product of Array Except Self
Given an integer array nums, return an array answer such that answer[i] is equal to the product of all
the elements of nums except nums[i].
The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
You must write an algorithm that runs in 0(n) time and without using the division operation.
Example 1:
Input: nums = [1,2,3,4]
Output: [24,12,8,6]
Example 2:
Input: nums = [-1,1,0,-3,3]
Output: [0,0,9,0,0]
Constraints:
2 <= nums.length <= 105
-30 <= nums[i] <= 30
The input is generated such that answer[i] is guaranteed to fit in a 32-bit
Solution:
C:
int* productExceptSelf(int* nums, int numsSize, int* returnSize) {
int* result = (int*)malloc(numsSize * sizeof(int));
*returnSize = numsSize;
return result;
}
Prepared by Kashish Zaveri, Samarth Joshi, Tarun Ray, Unnati Suryawanshi (T&P Coordinators) 21/03/2025
C++:
return result;
}
Java:
return result;
Prepared by Kashish Zaveri, Samarth Joshi, Tarun Ray, Unnati Suryawanshi (T&P Coordinators) 21/03/2025
Python:
Prepared by Kashish Zaveri, Samarth Joshi, Tarun Ray, Unnati Suryawanshi (T&P Coordinators) 21/03/2025
IT-2,3
A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all
non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters
include letters and numbers.
Given a string s, return true if it is a palindrome, or false otherwise.
Example 1:
Input: s = "A man, a plan, a canal: Panama"
Output: true
Explanation: "amanaplanacanalpanama" is a palindrome.
Example 2:
Input: s = "race a car"
Output: false
Explanation: "raceacar" is not a palindrome.
Constraints:
1 <= s.length <= 2 * 105 s consists only of printable
ASCII characters.
Solution:
C:
boolean isPalindrome(char* s)
{ int left = 0;
int right = strlen(s) - 1;
return true;
}
Prepared by Kashish Zaveri, Samarth Joshi, Tarun Ray, Unnati Suryawanshi (T&P Coordinators) 21/03/2025
C++:
Java:
return true;
}
Prepared by Kashish Zaveri, Samarth Joshi, Tarun Ray, Unnati Suryawanshi (T&P Coordinators) 21/03/2025
Python:
return True
Prepared by Kashish Zaveri, Samarth Joshi, Tarun Ray, Unnati Suryawanshi (T&P Coordinators) 21/03/2025
(2). Remove Nth Node From End of List
Given the head of a linked list, remove the nth node from the end of the list and return its head.
Example 1:
Input: head = [1,2,3,4,5], n = 2
Output: [1,2,3,5]
Example 2:
Input: head = [1], n = 1
Output: []
Constraints:
The number of nodes in the list is sz.
1 <= sz <= 30
0 <= Node.val <= 100
1 <= n <= sz Solution:
C:
return dummy.next;
}
Prepared by Kashish Zaveri, Samarth Joshi, Tarun Ray, Unnati Suryawanshi (T&P Coordinators) 21/03/2025
C++:
return dummy.next;
}
Java:
Prepared by Kashish Zaveri, Samarth Joshi, Tarun Ray, Unnati Suryawanshi (T&P Coordinators) 21/03/2025
Python:
fast = fast.next
return dummy.next
Prepared by Kashish Zaveri, Samarth Joshi, Tarun Ray, Unnati Suryawanshi (T&P Coordinators) 21/03/2025