Skip to content

Microsoft Coding Questions - DSA Problems & Solutions

Practice Microsoft placement paper coding questions with detailed solutions. Access Microsoft OA coding problems in C++, Java, Python, C#.

This page contains Microsoft coding questions from Microsoft OA placement papers with detailed solutions.

Microsoft OA Coding Section:

  • Problems: 3-4 coding problems
  • Time: 60-90 minutes
  • Languages: C++, Java, Python, C#
Q1: Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

Solution (C++):

vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> map;
for (int i = 0; i < nums.size(); i++) {
int complement = target - nums[i];
if (map.find(complement) != map.end()) {
return {map[complement], i};
}
map[nums[i]] = i;
}
return {};
}

Time Complexity: O(n)

Q2: Find the maximum path sum in a binary tree.

Solution (C++):

int maxPathSum(TreeNode* root) {
int maxSum = INT_MIN;
maxPathSumHelper(root, maxSum);
return maxSum;
}
int maxPathSumHelper(TreeNode* node, int& maxSum) {
if (!node) return 0;
int left = max(0, maxPathSumHelper(node->left, maxSum));
int right = max(0, maxPathSumHelper(node->right, maxSum));
maxSum = max(maxSum, node->val + left + right);
return node->val + max(left, right);
}

Time Complexity: O(n)


Practice Microsoft coding questions regularly!