You are on page 1of 7

ROUND 2

Q1) In a linked list of size n, where n is even, the ith node (0-indexed) of the linked list is known as
the twin of the (n-1-i)th node, if 0 <= i <= (n / 2) - 1.
 For example, if n = 4, then node 0 is the twin of node 3, and node 1 is the twin of node 2. These are
the only nodes with twins for n = 4.
The twin sum is defined as the sum of a node and its twin.
Given the head of a linked list with even length, return the maximum twin sum of the linked list.
 
Example 1:

Input: head = [5,4,2,1]


Output: 6
Explanation:
Nodes 0 and 1 are the twins of nodes 3 and 2, respectively. All have twin sum = 6.
There are no other nodes with twins in the linked list.
Thus, the maximum twin sum of the linked list is 6.

#include <vector>
#include <climits>
int twinSum(Node* head){
Node* temp = head ;
vector<int> arr ;
while(temp){
arr.push_back(temp->val) ;
temp = temp->next ;
}

int i = 0;
int j = arr.size() - 1 ;
int ans = INT_MIN ;
while(i<j){
if(arr[i]+arr[j] > ans)ans = arr[i]+arr[j] ;
i++;
j--;
}
return ans ;
}
Q2) Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the
nearest leaf node.
Note: A leaf is a node with no children.
 
Example 1:

Input: root = [3,9,20,null,null,15,7]


Output: 2
Example 2:
Input: root = [2,null,3,null,4,null,5,null,6]
Output: 5
 
Constraints:
 The number of nodes in the tree is in the range [0, 105].
 -1000 <= Node.val <= 1000

int minDepth(TreeNode* root) {


if(root==NULL)return 0;

int left = minDepth(root->left);


int right = minDepth(root->right);

if(left==0 && right!=0)


{
return right+1;
}
else if(right==0 && left!=0)
{
return left+1;
}
else{
return min(left,right) +1;
}
}
Q3) Given two strings s and t, determine if they are isomorphic.
Two strings s and t are isomorphic if the characters in s can be replaced to get t.
All occurrences of a character must be replaced with another character while preserving the order of
characters. No two characters may map to the same character, but a character may map to itself.
 
Example 1:
Input: s = "egg", t = "add"
Output: true
Example 2:
Input: s = "foo", t = "bar"
Output: false
Example 3:
Input: s = "paper", t = "title"
Output: true
 
Constraints:
 1 <= s.length <= 5 * 104
 t.length == s.length
 s and t consist of any valid ascii character.

bool isIsomorphic(string s, string t) {


int n=s.length();
int a[400]={0};
int b[400]={0};
for(int i=0;i<n;i++){
//Check if the new S[i] char has it T[i] already been used or not
if(a[s[i]+1]==0 && b[t[i]+1]==1){
return false;
}
//Check when S[i] has the same old used T[i] char or not
if(a[s[i]+1]!=0 && a[s[i]+1]!=t[i]+1){
return false;
}
a[s[i]+1]=t[i]+1;
b[t[i]+1]=1;
}
//else its supposed to be true bruhh
return true;
}
};
ROUND 3
Q1) Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two
nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be
a descendant of itself).”
 
Example 1:

Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1


Output: 3
Explanation: The LCA of nodes 5 and 1 is 3.

Node* lowestCommonAncestor(TreeNode* root, int p, int q) {


if(root == NULL)
return NULL;

if(root->val == p || root->val == q)
return root;

Node* left = lowestCommonAncestor(root->left, p, q);


Node* right = lowestCommonAncestor(root->right, p, q);

if(left != NULL && right == NULL)


return left;
if(left == NULL && right != NULL)
return right;
if(left != NULL && right != NULL)
return root;
else
return NULL;
}
};
Q2) Given a string of brackets, the task is to find an index k which decides the number of opening brackets
is equal to the number of closing brackets. The string must be consists of only opening and closing brackets
i.e. '(' and ')'.
Examples:
Input: str = “(())))(“
Output: 4
Explanation: After index 4, string splits into (()) and ))(. The number of opening brackets in the first part is
equal to the number of closing brackets in the second part.
Input: str = “))”
Output: 2
Explanation: As after 2nd position i.e. )) and “empty” string will be split into these two parts. So, in this
number of opening brackets i.e. 0 in the first part is equal to the number of closing brackets in the second
part i.e. also 0.

int findEqualPoint(string str) {


   int total_length = str.length();
   int open[total_length+1] = {0}, close[total_length+1] = {0};
   int index = -1;
   open[0] = 0;
   close[total_length] = 0;
   if (str[0]=='(') //check whether first bracket is opening or not, if so mark open[1] = 1
   open[1] = 1;
   if (str[total_length-1] == ')') //check whether first bracket is closing or not, if so mark       close[end] = 1
   close[total_length-1] = 1;
   for (int i = 1; i < total_length; i++) {
      if ( str[i] == '(' )
      open[i+1] = open[i] + 1;
   else
      open[i+1] = open[i];
   }
   for (int i = total_length-2; i >= 0; i--) {
      if ( str[i] == ')' )
         close[i] = close[i+1] + 1;
      else
         close[i] = close[i+1];
   }
   if (open[total_length] == 0)
      return total_length;
   if (close[0] == 0)
      return 0;
   for (int i=0; i<=total_length; i++)
      if (open[i] == close[i])
      index = i;
   return index;
}
Q3) You are given the head of a linked list.
The nodes in the linked list are sequentially assigned to non-empty groups whose lengths form the
sequence of the natural numbers (1, 2, 3, 4, ...). The length of a group is the number of nodes assigned to
it. In other words,
 The 1st node is assigned to the first group.
 The 2nd and the 3rd nodes are assigned to the second group.
 The 4th, 5th, and 6th nodes are assigned to the third group, and so on.
Note that the length of the last group may be less than or equal to 1 + the length of the second to last
group.
Reverse the nodes in each group with an even length, and return the head of the modified linked list.
 
Example 1:

Input: head = [5,2,6,3,9,1,7,3,8,4]


Output: [5,6,2,3,9,1,4,8,3,7]
Explanation:
- The length of the first group is 1, which is odd, hence no reversal occurs.
- The length of the second group is 2, which is even, hence the nodes are reversed.
- The length of the third group is 3, which is odd, hence no reversal occurs.
- The length of the last group is 4, which is even, hence the nodes are reversed.

ListNode* reverseEvenLengthGroups(ListNode* head) {


int n=0;
ListNode* temp = head;
while(temp)
{
n++;
temp = temp->next;
}
temp=head;
ListNode* prev,*curr,*preHead;
int i=1;
while(n)
{
if(n>=i)
n-=i;
else if(n<i&&n>0&&n%2==0){
i=n;
n=0;
}
else break;
// cout<<i<<" ";
if(i%2)
{
for(int j=0;j<i;j++){
curr=temp;
temp=temp->next;
}

}
else{
prev=NULL;
ListNode* tmp,*next;
for(int j=0;j<i;j++)
{
tmp = temp->next;
temp->next=prev;
if(!prev)
preHead=temp;
prev=temp;
temp=tmp;
}
curr->next=prev;
preHead->next=tmp;
curr=preHead;
}
i++;
}
return head;
}

You might also like