- L&T Infotech Coding Test Overview
- Key Topics for the Coding Test
- Top 5 Sample Q&A for L&T Infotech Coding Test
- Preparation Tips for L&T Infotech Coding Test
- How to Approach the Coding Test?
- Frequently Asked Questions (FAQs)
L&T Infotech Coding Questions & Answers with Tips for Freshers 2025
The L&T Infotech Coding Test is a critical step for aspiring candidates aiming to secure a position in one of the leading IT services companies. Whether you're a fresher looking to kickstart your career or an experienced professional preparing for a lateral entry, mastering the coding test can significantly enhance your chances of success.
This article offers an in-depth guide to L&T Infotech's coding test format, sample questions, preparation tips, and insights to help you excel.
L&T Infotech Coding Test Overview
The L&T Infotech coding test is designed to assess candidates' problem-solving abilities and coding proficiency. It typically evaluates programming skills, algorithmic thinking, and logical reasoning.
|
Feature |
Details |
|
Test Duration |
60–90 minutes |
|
Number of Questions |
2–3 coding problems |
|
Difficulty Level |
Moderate to Advanced |
|
Programming Languages |
C, C++, Java, Python, etc. |
|
Topics Covered |
Data Structures, Algorithms, Strings, Arrays, etc. |
Key Topics for the Coding Test
- Data Structures and Algorithms
- Arrays
- Linked Lists
- Stacks and Queues
- Binary Trees and Binary Search Trees
- String Manipulations
- Pattern Matching
- Anagram Detection
- Palindrome Verification
- Dynamic Programming
- Knapsack Problems
- Longest Common Subsequence
- Coin Change Problems
- Mathematical and Logical Problems
- Prime Number Checks
- Fibonacci Series
- Modular Arithmetic
- Database Queries (Optional)
- Basic SQL Queries
- Joins and Subqueries
Top 5 Sample Q&A for L&T Infotech Coding Test
Problem Statement 1
In a computer class, students are represented as nodes in a binary search tree (BST), with each student having a unique roll number. Given two nodes, x and y, in this BST, the task is to find their Lowest Common Ancestor (LCA). The LCA is the lowest node in the tree, and it has both x and y as descendants.
Input Format
The first line of the input contains an integer n denoting the number of nodes in the binary search tree.
The second line contains n space-separated integers denoting nodes of the binary search tree. Nodes are in the form of level order traversal of binary search trees.
Third line contains two space-separated integers that is x and y.
Output Format
Print an integer which is the lowest common ancestor (LCA) of nodes x and y.
Solution C++
#include <bits/stdc++.h>
using namespace std;
class TreeNode
{
public:
int data;
TreeNode* left = NULL, *right = NULL;
TreeNode() {}
TreeNode(int data): data(data) {}
};
TreeNode* insert(TreeNode* root, int key)
{
if (root == NULL) // if the root is null then return new node
{
return new TreeNode(key);
}
if (key < root->data) {
root->left = insert(root->left, key);
//if the value of key is less than root's value,
//then insert the node to the left
}
else {
root->right = insert(root->right, key);
//if the value of key is greater than root's value,
//then insert the node to the right
}
return root;
}
int lowestCommonAncestor(TreeNode* root, int p, int q) // function to find and return the founder
{
TreeNode* cur = root;
while (true) {
if (p < cur -> data && q < cur -> data)
{
cur = cur -> left; //move the curr pointer to left
}
else if (p > cur -> data && q > cur -> data)
{
cur = cur -> right; //move the curr pointer to right
}
else {
break;
}
}
return cur->data; //return the curr value
}
int main()
{
int n;
cin>>n; //taking input number of nodes
TreeNode*root=NULL;
for(int i=0;i<n;i++)
{
int a;
cin>>a; //taking input values of nodes
root=insert(root,a); //inserting value of nodes to form a tree
}
int x,y;
cin>>x>>y; //taking input value of 2 nodes a and b
cout<<lowestCommonAncestor(root,x,y);
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmNsYXNzIFRyZWVOb2RlCnsKICAgIHB1YmxpYzoKICAgIGludCBkYXRhOwogICAgVHJlZU5vZGUqIGxlZnQgPSBOVUxMLCAqcmlnaHQgPSBOVUxMOwogICAgVHJlZU5vZGUoKSB7fQogICAgVHJlZU5vZGUoaW50IGRhdGEpOiBkYXRhKGRhdGEpIHt9Cn07CiAKVHJlZU5vZGUqIGluc2VydChUcmVlTm9kZSogcm9vdCwgaW50IGtleSkKewogICAgaWYgKHJvb3QgPT0gTlVMTCkgIC8vIGlmIHRoZSByb290IGlzIG51bGwgdGhlbiByZXR1cm4gbmV3IG5vZGUKCXsKICAgICAgICByZXR1cm4gbmV3IFRyZWVOb2RlKGtleSk7CiAgICB9CiAgICBpZiAoa2V5IDwgcm9vdC0+ZGF0YSkgew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nQgbWFpbigpCnsKIAlpbnQgbjsKIAljaW4+Pm47IC8vdGFraW5nIGlucHV0IG51bWJlciBvZiBub2RlcwogCVRyZWVOb2RlKnJvb3Q9TlVMTDsKIAlmb3IoaW50IGk9MDtpPG47aSsrKQogCXsKIAkJaW50IGE7CiAJCWNpbj4+YTsgLy90YWtpbmcgaW5wdXQgdmFsdWVzIG9mIG5vZGVzCiAJCXJvb3Q9aW5zZXJ0KHJvb3QsYSk7IC8vaW5zZXJ0aW5nIHZhbHVlIG9mIG5vZGVzIHRvIGZvcm0gYSB0cmVlCgl9CglpbnQgeCx5OwoJY2luPj54Pj55OyAvL3Rha2luZyBpbnB1dCB2YWx1ZSBvZiAyIG5vZGVzIGEgYW5kIGIKCWNvdXQ8PGxvd2VzdENvbW1vbkFuY2VzdG9yKHJvb3QseCx5KTsKICAgIHJldHVybiAwOwp9Cg==
Solution Java
import java.util.Scanner;
class TreeNode {
int data;
TreeNode left, right;
TreeNode(int data) {
this.data = data;
this.left = null;
this.right = null;
}
}
public class Main {
public static TreeNode insert(TreeNode root, int key) {
if (root == null) { // if the root is null then return new node
return new TreeNode(key);
}
if (key < root.data) {
root.left = insert(root.left, key);
// if the value of key is less than root's value, then insert the node to the left
} else {
root.right = insert(root.right, key);
// if the value of key is greater than root's value, then insert the node to the right
}
return root;
}
public static int lowestCommonAncestor(TreeNode root, int p, int q) {
TreeNode cur = root;
while (true) {
if (p < cur.data && q < cur.data) {
cur = cur.left; // move the cur pointer to left
} else if (p > cur.data && q > cur.data) {
cur = cur.right; // move the cur pointer to right
} else {
break;
}
}
return cur.data; // return the cur value
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(); // taking input number of nodes
TreeNode root = null;
for (int i = 0; i < n; i++) {
int a = sc.nextInt(); // taking input values of nodes
root = insert(root, a); // inserting value of nodes to form a tree
}
int x = sc.nextInt(); // taking input value of node a
int y = sc.nextInt(); // taking input value of node b
System.out.println(lowestCommonAncestor(root, x, y));
sc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ZGUgcm9vdCwgaW50IHAsIGludCBxKSB7CiAgICAgICAgVHJlZU5vZGUgY3VyID0gcm9vdDsKICAgICAgICB3aGlsZSAodHJ1ZSkgewogICAgICAgICAgICBpZiAocCA8IGN1ci5kYXRhICYmIHEgPCBjdXIuZGF0YSkgewogICAgICAgICAgICAgICAgY3VyID0gY3VyLmxlZnQ7IC8vIG1vdmUgdGhlIGN1ciBwb2ludGVyIHRvIGxlZnQKICAgICAgICAgICAgfSBlbHNlIGlmIChwID4gY3VyLmRhdGEgJiYgcSA+IGN1ci5kYXRhKSB7CiAgICAgICAgICAgICAgICBjdXIgPSBjdXIucmlnaHQ7IC8vIG1vdmUgdGhlIGN1ciBwb2ludGVyIHRvIHJpZ2h0CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXR1cm4gY3VyLmRhdGE7IC8vIHJldHVybiB0aGUgY3VyIHZhbHVlCiAgICB9CgogICAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgewogICAgICAgIFNjYW5uZXIgc2MgPSBuZXcgU2Nhbm5lcihTeXN0ZW0uaW4pOwogICAgICAgIGludCBuID0gc2MubmV4dEludCgpOyAvLyB0YWtpbmcgaW5wdXQgbnVtYmVyIG9mIG5vZGVzCiAgICAgICAgVHJlZU5vZGUgcm9vdCA9IG51bGw7CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICAgICAgaW50IGEgPSBzYy5uZXh0SW50KCk7IC8vIHRha2luZyBpbnB1dCB2YWx1ZXMgb2Ygbm9kZXMKICAgICAgICAgICAgcm9vdCA9IGluc2VydChyb290LCBhKTsgLy8gaW5zZXJ0aW5nIHZhbHVlIG9mIG5vZGVzIHRvIGZvcm0gYSB0cmVlCiAgICAgICAgfQogICAgICAgIGludCB4ID0gc2MubmV4dEludCgpOyAvLyB0YWtpbmcgaW5wdXQgdmFsdWUgb2Ygbm9kZSBhCiAgICAgICAgaW50IHkgPSBzYy5uZXh0SW50KCk7IC8vIHRha2luZyBpbnB1dCB2YWx1ZSBvZiBub2RlIGIKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4obG93ZXN0Q29tbW9uQW5jZXN0b3Iocm9vdCwgeCwgeSkpOwogICAgICAgIHNjLmNsb3NlKCk7CiAgICB9Cn0K
Solution Python
class TreeNode:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def insert_level_order(arr, root, i, n):
# Base case for recursion
if i < n:
temp = TreeNode(arr[i])
root = temp
# Insert left child
root.left = insert_level_order(arr, root.left, 2 * i + 1, n)
# Insert right child
root.right = insert_level_order(arr, root.right, 2 * i + 2, n)
return root
def lowest_common_ancestor(root, p, q):
cur = root
while cur:
if p < cur.data and q < cur.data:
cur = cur.left # Move to the left child
elif p > cur.data and q > cur.data:
cur = cur.right # Move to the right child
else:
return cur.data # This is the LCA
return None
def main():
# Reading inputs
n = int(input()) # Number of nodes
if n == 0:
return
nodes = list(map(int, input().split())) # Node values in level order traversal
x, y = map(int, input().split()) # Nodes to find the LCA for
root = insert_level_order(nodes, None, 0, n) # Build the BST from level order input
# Find and print the LCA of x and y
print(lowest_common_ancestor(root, x, y))
if __name__ == "__main__":
main()
Y2xhc3MgVHJlZU5vZGU6DQogICAgZGVmIF9faW5pdF9fKHNlbGYsIGRhdGEpOg0KICAgICAgICBzZWxmLmRhdGEgPSBkYXRhDQogICAgICAgIHNlbGYubGVmdCA9IE5vbmUNCiAgICAgICAgc2VsZi5yaWdodCA9IE5vbmUNCg0KZGVmIGluc2VydF9sZXZlbF9vcmRlcihhcnIsIHJvb3QsIGksIG4pOg0KICAgICMgQmFzZSBjYXNlIGZvciByZWN1cnNpb24NCiAgICBpZiBpIDwgbjoNCiAgICAgICAgdGVtcCA9IFRyZWVOb2RlKGFycltpXSkNCiAgICAgICAgcm9vdCA9IHRlbXANCg0KICAgICAgICAjIEluc2VydCBsZ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ogICAgcm9vdCA9IGluc2VydF9sZXZlbF9vcmRlcihub2RlcywgTm9uZSwgMCwgbikgICMgQnVpbGQgdGhlIEJTVCBmcm9tIGxldmVsIG9yZGVyIGlucHV0DQoNCiAgICAjIEZpbmQgYW5kIHByaW50IHRoZSBMQ0Egb2YgeCBhbmQgeQ0KICAgIHByaW50KGxvd2VzdF9jb21tb25fYW5jZXN0b3Iocm9vdCwgeCwgeSkpDQoNCmlmIF9fbmFtZV9fID09ICJfX21haW5fXyI6DQogICAgbWFpbigpDQo=
Problem Statement 2
Ram had an array of integers. He wants to know whether he can make an arithmetic progression using this sequence or not.
Note: You have to include all elements. Numbers present in the array are unsorted.
Help Ram in finding whether he can make an arithmetic progression using sequence or not.
Return true if he can make a sequence using this; otherwise, it is false.
Input Format
The first line contains the size of the array,i.e. n.
Second line n space-separated integers.
Output Format
true or false.
Solution C++
#include<bits/stdc++.h>
using namespace std;
bool canMakeAP(vector &nums)
{
unordered_set mp;
for(auto n: nums) mp.insert(n);
int maxm = *max_element(nums.begin(), nums.end());
int minm = *min_element(nums.begin(), nums.end());
int diff = (maxm - minm)/(nums.size()-1);
for(int i=1;i<nums.size();i++){
if(mp.find(minm + i*diff) == mp.end()) return false;
}
return true;
}
int main()
{
int n;
cin>>n;
vector v(n);
for(int i=0;i<n;i++) cin>>v[i];
bool ans=canMakeAP(v);
if(ans) cout<<"true";
else cout<<"false";
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKYm9vbCBjYW5NYWtlQVAodmVjdG9yPGludD4gJm51bXMpCnsKICAgICB1bm9yZGVyZWRfc2V0PGludD4gbXA7CiAgICAgICAgZm9yKGF1dG8gbjogbnVtcykgbXAuaW5zZXJ0KG4pOwogICAgICAgIGludCBtYXhtID0gKm1heF9lbGVtZW50KG51bXMuYmVnaW4oKSwgbnVtcy5lbmQoKSk7CiAgICAgICAgaW50IG1pbm0gPSAqbWluX2VsZW1lbnQobnVtcy5iZWdpbigpLCBudW1zLmVuZCgpKTsKICAgICAgICBpbnQgZGlmZiA9IChtYXhtIC0gbWlubSkvKG51bXMuc2l6ZSgpLTEpOwogICAgICAgIGZvcihpbnQgaT0xO2k8bnVtcy5zaXplKCk7aSsrKXsKICAgICAgICAgICAgaWYobXAuZmluZChtaW5tICsgaSpkaWZmKSA9PSBtcC5lbmQoKSkgcmV0dXJuIGZhbHNlOwogICAgICAgIH0KICAgICAgICByZXR1cm4gdHJ1ZTsKfQppbnQgbWFpbigpCnsKICAgIGludCBuOwogICAgY2luPj5uOwogICAgdmVjdG9yPGludD4gdihuKTsKICAgIGZvcihpbnQgaT0wO2k8bjtpKyspIGNpbj4+dltpXTsKICAgIGJvb2wgYW5zPWNhbk1ha2VBUCh2KTsKICAgIGlmKGFucykgY291dDw8InRydWUiOwogICAgZWxzZSBjb3V0PDwiZmFsc2UiOwogICAgcmV0dXJuIDA7Cn0K
Solution Java
import java.util.*;
public class Main {
public static boolean canMakeAP(int[] nums) {
HashSet set = new HashSet<>();
for (int num : nums) {
set.add(num);
}
int max = Arrays.stream(nums).max().getAsInt();
int min = Arrays.stream(nums).min().getAsInt();
int n = nums.length;
// If the difference isn't an integer, we can't form an AP
if ((max - min) % (n - 1) != 0) {
return false;
}
int diff = (max - min) / (n - 1);
for (int i = 1; i < n; i++) {
if (!set.contains(min + i * diff)) {
return false;
}
}
return true;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = sc.nextInt();
}
boolean ans = canMakeAP(nums);
if (ans) {
System.out.println("true");
} else {
System.out.println("false");
}
}
}
aW1wb3J0IGphdmEudXRpbC4qOwoKcHVibGljIGNsYXNzIE1haW4gewogICAgcHVibGljIHN0YXRpYyBib29sZWFuIGNhbk1ha2VBUChpbnRbXSBudW1zKSB7CiAgICAgICAgSGFzaFNldDxJbnRlZ2VyPiBzZXQgPSBuZXcgSGFzaFNldDw+KCk7CiAgICAgICAgZm9yIChpbnQgbnVtIDogbnVtcykgewogICAgICAgICAgICBzZXQuYWRkKG51bSk7CiAgICAgICAgfQoKICAgICAgICBpbnQgbWF4ID0gQXJyYXlzLnN0cmVhbShudW1zKS5tYXgoKS5nZXRBc0ludCgpOwogICAgICAgIGludCBtaW4gPSBBcnJheXMuc3RyZWFtKG51bXMpLm1pbigpLmdldEFzSW50KCk7CiAgICAgICAgaW50IG4gPSBudW1zLmxlbmd0aDsKCiAgICAgICAgLy8gSWYgdGhlIGRpZmZlcmVuY2UgaXNuJ3QgYW4gaW50ZWdlciwgd2UgY2FuJ3QgZm9ybSBhbiBBUAogICAgICAgIGlmICgobWF4IC0gbWluKSAlIChuIC0gMSkgIT0gMCkgewogICAgICAgICAgICByZXR1cm4gZmFsc2U7CiAgICAgICAgfQoKICAgICAgICBpbnQgZGlmZiA9IChtYXggLSBtaW4pIC8gKG4gLSAxKTsKCiAgICAgICAgZm9yIChpbnQgaSA9IDE7IGkgPCBuOyBpKyspIHsKICAgICAgICAgICAgaWYgKCFzZXQuY29udGFpbnMobWluICsgaSAqIGRpZmYpKSB7CiAgICAgICAgICAgICAgICByZXR1cm4gZmFsc2U7CiAgICAgICAgICAgIH0KICAgICAgICB9CgogICAgICAgIHJldHVybiB0cnVlOwogICAgfQoKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKICAgICAgICBTY2FubmVyIHNjID0gbmV3IFNjYW5uZXIoU3lzdGVtLmluKTsKCiAgICAgICAgaW50IG4gPSBzYy5uZXh0SW50KCk7CiAgICAgICAgaW50W10gbnVtcyA9IG5ldyBpbnRbbl07CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICAgICAgbnVtc1tpXSA9IHNjLm5leHRJbnQoKTsKICAgICAgICB9CgogICAgICAgIGJvb2xlYW4gYW5zID0gY2FuTWFrZUFQKG51bXMpOwogICAgICAgIGlmIChhbnMpIHsKICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJ0cnVlIik7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJmYWxzZSIpOwogICAgICAgIH0KICAgIH0KfQo=
Solution Python
def can_make_ap(nums):
num_set = set(nums)
max_num = max(nums)
min_num = min(nums)
n = len(nums)
# If the difference isn't an integer, we can't form an AP
if (max_num - min_num) % (n - 1) != 0:
return False
diff = (max_num - min_num) // (n - 1)
for i in range(1, n):
if (min_num + i * diff) not in num_set:
return False
return True
if __name__ == "__main__":
n = int(input())
nums = list(map(int, input().split()))
if can_make_ap(nums):
print("true")
else:
print("false")
ZGVmIGNhbl9tYWtlX2FwKG51bXMpOg0KICAgIG51bV9zZXQgPSBzZXQobnVtcykNCiAgICBtYXhfbnVtID0gbWF4KG51bXMpDQogICAgbWluX251bSA9IG1pbihudW1zKQ0KICAgIG4gPSBsZW4obnVtcykNCg0KICAgICMgSWYgdGhlIGRpZmZlcmVuY2UgaXNuJ3QgYW4gaW50ZWdlciwgd2UgY2FuJ3QgZm9ybSBhbiBBUA0KICAgIGlmIChtYXhfbnVtIC0gbWluX251bSkgJSAobiAtIDEpICE9IDA6DQogICAgICAgIHJldHVybiBGYWxzZQ0KDQogICAgZGlmZiA9IChtYXhfbnVtIC0gbWluX251bSkgLy8gKG4gLSAxKQ0KDQogICAgZm9yIGkgaW4gcmFuZ2UoMSwgbik6DQogICAgICAgIGlmIChtaW5fbnVtICsgaSAqIGRpZmYpIG5vdCBpbiBudW1fc2V0Og0KICAgICAgICAgICAgcmV0dXJuIEZhbHNlDQoNCiAgICByZXR1cm4gVHJ1ZQ0KDQoNCmlmIF9fbmFtZV9fID09ICJfX21haW5fXyI6DQogICAgbiA9IGludChpbnB1dCgpKQ0KICAgIG51bXMgPSBsaXN0KG1hcChpbnQsIGlucHV0KCkuc3BsaXQoKSkpDQogICAgaWYgY2FuX21ha2VfYXAobnVtcyk6DQogICAgICAgIHByaW50KCJ0cnVlIikNCiAgICBlbHNlOg0KICAgICAgICBwcmludCgiZmFsc2UiKQ0K
Problem Statement 3
You are given a sequence of N tiles, each of which is either Red or Green. You are also provided with an integer K, which indicates the number of consecutive Red tiles you need. In each operation, you can paint one tile Red, using one box of paint per tile.
Your task is to determine the minimum number of paint boxes required to achieve at least K consecutive Red tiles in the given sequence.
Input Format
The first line contains two space-separated integers: N represents the total number of tiles, and K represents the number of consecutive Red tiles required.
The second line contains a string of length N where each character is either 'R' or 'G'.
Output Format
Print a single integer representing the minimum number of paint boxes needed.
Solution C++
#include<bits/stdc++.h>
using namespace std;
int minimumRecolors(string blocks, int k) {
int n = blocks.size(), minOps = 1e9, flips = 0, count = 0, i = 0;
for(int j = 0; j < n; ++j) {
if(blocks[j] == 'G') {
flips++;
count++;
} else if(blocks[j] == 'R') {
count++;
}
if(count == k) {
minOps = min(minOps, flips);
if(blocks[i] == 'G') {
flips--;
count--;
} else count--;
i++;
}
}
return minOps;
}
int main(){
int n , k ;
cin >> n >> k ;
string str;
cin >> str;
cout << minimumRecolors(str, k);
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4gCnVzaW5nIG5hbWVzcGFjZSBzdGQ7IAogIGludCBtaW5pbXVtUmVjb2xvcnMoc3RyaW5nIGJsb2NrcywgaW50IGspIHsKICAgICAgICBpbnQgbiA9IGJsb2Nrcy5zaXplKCksIG1pbk9wcyA9IDFlOSwgZmxpcHMgPSAwLCBjb3VudCA9IDAsIGkgPSAwOwogICAgICAgIGZvcihpbnQgaiA9IDA7IGogPCBuOyArK2opIHsKICAgICAgICAgICAgaWYoYmxvY2tzW2pdID09ICdHJykgewogICAgICAgICAgICAgICAgZmxpcHMrKzsKICAgICAgICAgICAgICAgIGNvdW50Kys7CiAgICAgICAgICAgIH0gZWxzZSBpZihibG9ja3Nbal0gPT0gJ1InKSB7CiAgICAgICAgICAgICAgICBjb3VudCsrOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmKGNvdW50ID09IGspIHsKICAgICAgICAgICAgICAgIG1pbk9wcyA9IG1pbihtaW5PcHMsIGZsaXBzKTsKICAgICAgICAgICAgICAgIGlmKGJsb2Nrc1tpXSA9PSAnRycpIHsKICAgICAgICAgICAgICAgICAgICBmbGlwcy0tOwogICAgICAgICAgICAgICAgICAgIGNvdW50LS07CiAgICAgICAgICAgICAgICB9IGVsc2UgY291bnQtLTsKICAgICAgICAgICAgICAgIGkrKzsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXR1cm4gbWluT3BzOwogICAgfQogaW50IG1haW4oKXsgCiAgIAogICBpbnQgbiAsIGsgOyAKICAgICBjaW4gPj4gbiA+PiBrIDsgCiAgICAgCiAgICAgIHN0cmluZyBzdHI7IAogICAgICBjaW4gPj4gc3RyOwogICAgIGNvdXQgPDwgIG1pbmltdW1SZWNvbG9ycyhzdHIsIGspOwogICAKICAgIHJldHVybiAwOyAKIH0K
Solution Java
import java.util.Scanner;
public class Main {
public static int minimumRecolors(String blocks, int k) {
int n = blocks.length();
int minOps = Integer.MAX_VALUE;
int flips = 0;
int count = 0;
int i = 0;
for (int j = 0; j < n; ++j) {
if (blocks.charAt(j) == 'G') {
flips++;
count++;
} else if (blocks.charAt(j) == 'R') {
count++;
}
if (count == k) {
minOps = Math.min(minOps, flips);
if (blocks.charAt(i) == 'G') {
flips--;
count--;
} else {
count--;
}
i++;
}
}
return minOps;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int k = scanner.nextInt();
scanner.nextLine(); // Consume the newline
String str = scanner.nextLine();
scanner.close();
System.out.println(minimumRecolors(str, k));
}
}
aW1wb3J0IGphdmEudXRpbC5TY2FubmVyOwoKcHVibGljIGNsYXNzIE1haW4gewoKICAgIHB1YmxpYyBzdGF0aWMgaW50IG1pbmltdW1SZWNvbG9ycyhTdHJpbmcgYmxvY2tzLCBpbnQgaykgewogICAgICAgIGludCBuID0gYmxvY2tzLmxlbmd0aCgpOwogICAgICAgIGludCBtaW5PcHMgPSBJbnRlZ2VyLk1BWF9WQUxVRTsKICAgICAgICBpbnQgZmxpcHMgPSAwOwogICAgICAgIGludCBjb3VudCA9IDA7CiAgICAgICAgaW50IGkgPSAwOwoKICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IG47ICsraikgewogICAgICAgICAgICBpZiAoYmxvY2tzLmNoYXJBdChqKSA9PSAnRycpIHsKICAgICAgICAgICAgICAgIGZsaXBzKys7CiAgICAgICAgICAgICAgICBjb3VudCsrOwogICAgICAgICAgICB9IGVsc2UgaWYgKGJsb2Nrcy5jaGFyQXQoaikgPT0gJ1InKSB7CiAgICAgICAgICAgICAgICBjb3VudCsrOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChjb3VudCA9PSBrKSB7CiAgICAgICAgICAgICAgICBtaW5PcHMgPSBNYXRoLm1pbihtaW5PcHMsIGZsaXBzKTsKICAgICAgICAgICAgICAgIGlmIChibG9ja3MuY2hhckF0KGkpID09ICdHJykgewogICAgICAgICAgICAgICAgICAgIGZsaXBzLS07CiAgICAgICAgICAgICAgICAgICAgY291bnQtLTsKICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgY291bnQtLTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGkrKzsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXR1cm4gbWluT3BzOwogICAgfQoKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKICAgICAgICBTY2FubmVyIHNjYW5uZXIgPSBuZXcgU2Nhbm5lcihTeXN0ZW0uaW4pOwogICAgICAgIGludCBuID0gc2Nhbm5lci5uZXh0SW50KCk7CiAgICAgICAgaW50IGsgPSBzY2FubmVyLm5leHRJbnQoKTsKICAgICAgICBzY2FubmVyLm5leHRMaW5lKCk7IC8vIENvbnN1bWUgdGhlIG5ld2xpbmUKICAgICAgICBTdHJpbmcgc3RyID0gc2Nhbm5lci5uZXh0TGluZSgpOwogICAgICAgIHNjYW5uZXIuY2xvc2UoKTsKCiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKG1pbmltdW1SZWNvbG9ycyhzdHIsIGspKTsKICAgIH0KfQo=
Solution Python
def minimum_recolors(blocks: str, k: int) -> int:
n = len(blocks)
min_ops = float('inf')
flips = 0
count = 0
i = 0
for j in range(n):
if blocks[j] == 'G':
flips += 1
count += 1
else:
count += 1
if count == k:
min_ops = min(min_ops, flips)
if blocks[i] == 'G':
flips -= 1
count -= 1
else:
count -= 1
i += 1
return min_ops
def main():
import sys
input = sys.stdin.read
data = input().split()
n = int(data[0])
k = int(data[1])
blocks = data[2]
print(minimum_recolors(blocks, k))
if __name__ == "__main__":
main()
ZGVmIG1pbmltdW1fcmVjb2xvcnMoYmxvY2tzOiBzdHIsIGs6IGludCkgLT4gaW50Og0KICAgIG4gPSBsZW4oYmxvY2tzKQ0KICAgIG1pbl9vcHMgPSBmbG9hdCgnaW5mJykNCiAgICBmbGlwcyA9IDANCiAgICBjb3VudCA9IDANCiAgICBpID0gMA0KDQogICAgZm9yIGogaW4gcmFuZ2Uobik6DQogICAgICAgIGlmIGJsb2Nrc1tqXSA9PSAnRyc6DQogICAgICAgICAgICBmbGlwcyArPSAxDQogICAgICAgICAgICBjb3VudCArPSAxDQogICAgICAgIGVsc2U6DQogICAgICAgICAgICBjb3VudCArPSAxDQogICAgICAgIA0KICAgICAgICBpZiBjb3VudCA9PSBrOg0KICAgICAgICAgICAgbWluX29wcyA9IG1pbihtaW5fb3BzLCBmbGlwcykNCiAgICAgICAgICAgIGlmIGJsb2Nrc1tpXSA9PSAnRyc6DQogICAgICAgICAgICAgICAgZmxpcHMgLT0gMQ0KICAgICAgICAgICAgICAgIGNvdW50IC09IDENCiAgICAgICAgICAgIGVsc2U6DQogICAgICAgICAgICAgICAgY291bnQgLT0gMQ0KICAgICAgICAgICAgaSArPSAxDQoNCiAgICByZXR1cm4gbWluX29wcw0KDQpkZWYgbWFpbigpOg0KICAgIGltcG9ydCBzeXMNCiAgICBpbnB1dCA9IHN5cy5zdGRpbi5yZWFkDQogICAgZGF0YSA9IGlucHV0KCkuc3BsaXQoKQ0KICAgIA0KICAgIG4gPSBpbnQoZGF0YVswXSkNCiAgICBrID0gaW50KGRhdGFbMV0pDQogICAgYmxvY2tzID0gZGF0YVsyXQ0KICAgIA0KICAgIHByaW50KG1pbmltdW1fcmVjb2xvcnMoYmxvY2tzLCBrKSkNCg0KaWYgX19uYW1lX18gPT0gIl9fbWFpbl9fIjoNCiAgICBtYWluKCkNCg==
Problem Statement 4
Ram gave Shyaam a challenge; he gave Shyaam the head of a linked list and an integer K. He asked Shyaam to swap the values of the Kth node from the beginning and the Kth node from the end (the list is 1-indexed).
Note: The number of nodes in the list is N.
Input Format
The first line contains an integer N, representing the number of nodes in the linked list.
The second line contains N space-separated integers, each representing the value of a node in the linked list.
The third line contains an integer K, indicating the positions of the nodes to be swapped.
Output Format
Output the linked list after swapping the values of the two specified nodes.
Solution C++
#include<bits/stdc++.h>
using namespace std;
class ListNode{
public:
int val;
ListNode* next;
ListNode(){
this->val=0;
this->next=NULL;
}
};
ListNode* swapNodes(ListNode* head, int k) {
ListNode *ptr1 = head, *ptr2 = head, *kth = NULL;
while (--k)
ptr1 = ptr1->next;
kth = ptr1;
ptr1 = ptr1->next;
while (ptr1) {
ptr1 = ptr1->next;
ptr2 = ptr2->next;
}
swap(ptr2->val, kth->val);
return head;
}
void solve()
{
int n;
cin>>n;
int a=0;
ListNode* head=new ListNode();
ListNode* temp=head;
for(int i=0;i<n;i++){
cin>>a;
ListNode* newNode = new ListNode();
newNode->val=a;
temp->next=newNode;
temp=temp->next;
}
int k;cin>>k;
head=head->next;
head=swapNodes(head,k);
while(head){
cout<val<<" ";
head=head->next;
}
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD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gICB3aGlsZShoZWFkKXsKICAgICAgICBjb3V0PDxoZWFkLT52YWw8PCIgIjsKICAgICAgICBoZWFkPWhlYWQtPm5leHQ7CiAgICB9Cn0Kc2lnbmVkIG1haW4oKXsgCglpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTsKICAgIGNpbi50aWUoTlVMTCk7IAogICAgc29sdmUoKTsKICAgIAogICAgcmV0dXJuIDA7Cn0K
Solution Java
import java.util.Scanner;
class ListNode {
int val;
ListNode next;
ListNode() {
this.val = 0;
this.next = null;
}
ListNode(int val) {
this.val = val;
this.next = null;
}
}
public class Main {
public static ListNode swapNodes(ListNode head, int k) {
ListNode ptr1 = head, ptr2 = head, kth = null;
// Move `ptr1` to the k-th node
for (int i = 1; i < k; i++) {
ptr1 = ptr1.next;
}
kth = ptr1; // Store the k-th node
ptr1 = ptr1.next;
// Move `ptr2` to the k-th node from the end
while (ptr1 != null) {
ptr1 = ptr1.next;
ptr2 = ptr2.next;
}
// Swap values of the k-th node from start and end
int temp = kth.val;
kth.val = ptr2.val;
ptr2.val = temp;
return head;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
ListNode head = new ListNode();
ListNode temp = head;
for (int i = 0; i < n; i++) {
int val = sc.nextInt();
ListNode newNode = new ListNode(val);
temp.next = newNode;
temp = temp.next;
}
int k = sc.nextInt();
head = head.next; // Skip the dummy node
head = swapNodes(head, k);
// Print the modified list
while (head != null) {
System.out.print(head.val + " ");
head = head.next;
}
}
}
aW1wb3J0IGphdmEudXRpbC5TY2FubmVyOwoKY2xhc3MgTGlzdE5vZGUgewogICAgaW50IHZhbDsKICAgIExpc3ROb2RlIG5leHQ7CgogICAgTGlzdE5vZGUoKSB7CiAgICAgICAgdGhpcy52YWwgPSAwOwogICAgICAgIHRoaXMubmV4dCA9IG51bGw7CiAgICB9CgogICAgTGlzdE5vZGUoaW50IHZhbCkgewogICAgICAgIHRoaXMudmFsID0gdmFsOwogICAgICAgIH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iAgICAgICAgTGlzdE5vZGUgdGVtcCA9IGhlYWQ7CgogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgICAgIGludCB2YWwgPSBzYy5uZXh0SW50KCk7CiAgICAgICAgICAgIExpc3ROb2RlIG5ld05vZGUgPSBuZXcgTGlzdE5vZGUodmFsKTsKICAgICAgICAgICAgdGVtcC5uZXh0ID0gbmV3Tm9kZTsKICAgICAgICAgICAgdGVtcCA9IHRlbXAubmV4dDsKICAgICAgICB9CgogICAgICAgIGludCBrID0gc2MubmV4dEludCgpOwogICAgICAgIGhlYWQgPSBoZWFkLm5leHQ7ICAvLyBTa2lwIHRoZSBkdW1teSBub2RlCiAgICAgICAgaGVhZCA9IHN3YXBOb2RlcyhoZWFkLCBrKTsKCiAgICAgICAgLy8gUHJpbnQgdGhlIG1vZGlmaWVkIGxpc3QKICAgICAgICB3aGlsZSAoaGVhZCAhPSBudWxsKSB7CiAgICAgICAgICAgIFN5c3RlbS5vdXQucHJpbnQoaGVhZC52YWwgKyAiICIpOwogICAgICAgICAgICBoZWFkID0gaGVhZC5uZXh0OwogICAgICAgIH0KICAgIH0KfQo=
Solution Python
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def swapNodes(head, k):
ptr1 = head
ptr2 = head
kth = None
# Move ptr1 to the kth node
for _ in range(k - 1):
ptr1 = ptr1.next
# Mark the kth node
kth = ptr1
ptr1 = ptr1.next
# Move ptr1 to the end, while moving ptr2
while ptr1:
ptr1 = ptr1.next
ptr2 = ptr2.next
# Swap the values
kth.val, ptr2.val = ptr2.val, kth.val
return head
def solve():
n = int(input())
values = list(map(int, input().split()))
# Create the linked list
head = ListNode()
temp = head
for value in values:
new_node = ListNode(val=value)
temp.next = new_node
temp = temp.next
k = int(input())
# Skip the initial dummy node
head = head.next
head = swapNodes(head, k)
# Print the modified linked list
while head:
print(head.val, end=" ")
head = head.next
if __name__ == "__main__":
solve()
Y2xhc3MgTGlzdE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Xh0DQogICAgDQogICAgayA9IGludChpbnB1dCgpKQ0KICAgIA0KICAgICMgU2tpcCB0aGUgaW5pdGlhbCBkdW1teSBub2RlDQogICAgaGVhZCA9IGhlYWQubmV4dA0KICAgIGhlYWQgPSBzd2FwTm9kZXMoaGVhZCwgaykNCiAgICANCiAgICAjIFByaW50IHRoZSBtb2RpZmllZCBsaW5rZWQgbGlzdA0KICAgIHdoaWxlIGhlYWQ6DQogICAgICAgIHByaW50KGhlYWQudmFsLCBlbmQ9IiAiKQ0KICAgICAgICBoZWFkID0gaGVhZC5uZXh0DQoNCmlmIF9fbmFtZV9fID09ICJfX21haW5fXyI6DQogICAgc29sdmUoKQ0K
Problem Statement 5
You are given a string S. You have to sort the string so that the character with the greatest number of repetitions comes first. If there are multiple characters with a same number of repetitions, sort them in lexicographically.
Input Format
The first line contains String S.
Output Format
You have to print the string after sorting.
Solution C++
#include<bits/stdc++.h>
using namespace std;
const int mxn=1e5;
map<char,int>mp;
int main(){
string s;
cin>>s;int n=s.size();
for(int i=0;i<n;i++){
mp[s[i]]++;
}
map<int,vector>mp2;
vectorans;
for(auto i:mp){
mp2[i.second].push_back(i.first);
ans.push_back(i.second);
}
sort(ans.begin(),ans.end(),greater());
ans.resize(unique(ans.begin(),ans.end())-ans.begin());
for(int i=0;i<(int)ans.size();i++){
vectortemp=mp2[ans[i]];
sort(temp.begin(),temp.end());
for(int k=0;k<(int)temp.size();k++){
for(int j=0;j<ans[i];j++){
cout<<temp[k];
}
}
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKY29uc3QgaW50IG14bj0xZTU7Cm1hcDxjaGFyLGludD5tcDsKaW50IG1haW4oKXsKCXN0cmluZyBzOwoJY2luPj5zO2ludCBuPXMuc2l6ZSgpOwoJZm9yKGludCBpPTA7aTxuO2krKyl7CgkJbXBbc1tpXV0rKzsKCX0KCW1hcDxpbnQsdmVjdG9yPGNoYXI+Pm1wMjsKCXZlY3RvcjxpbnQ+YW5zOwoJZm9yKGF1dG8gaTptcCl7CgkJbXAyW2kuc2Vjb25kXS5wdXNoX2JhY2soaS5maXJzdCk7CgkJYW5zLnB1c2hfYmFjayhpLnNlY29uZCk7Cgl9Cglzb3J0KGFucy5iZWdpbigpLGFucy5lbmQoKSxncmVhdGVyPGludD4oKSk7CglhbnMucmVzaXplKHVuaXF1ZShhbnMuYmVnaW4oKSxhbnMuZW5kKCkpLWFucy5iZWdpbigpKTsKCWZvcihpbnQgaT0wO2k8KGludClhbnMuc2l6ZSgpO2krKyl7CgkJdmVjdG9yPGNoYXI+dGVtcD1tcDJbYW5zW2ldXTsKCQlzb3J0KHRlbXAuYmVnaW4oKSx0ZW1wLmVuZCgpKTsKCQlmb3IoaW50IGs9MDtrPChpbnQpdGVtcC5zaXplKCk7aysrKXsKCQkJZm9yKGludCBqPTA7ajxhbnNbaV07aisrKXsKCQkJCWNvdXQ8PHRlbXBba107CgkJCX0KCQl9Cgl9Cn0K
Solution Java
import java.util.*;
public class Main {
public static String sortString(String s) {
// User logic goes here
Map<Character, Integer> m = new HashMap<>();
for (char ch : s.toCharArray()) {
m.put(ch,m.getOrDefault(ch, 0) + 1);
}
List<Map.Entry<Character, Integer>> li = new ArrayList<>(m.entrySet());
li.sort((entry1, entry2) -> {
int frequencyCompare = Integer.compare(entry2.getValue(), entry1.getValue());
if (frequencyCompare == 0) {
return Character.compare(entry1.getKey(), entry2.getKey());
}
return frequencyCompare;
});
StringBuilder sb = new StringBuilder();
for (Map.Entry<Character, Integer> entry : li) {
char ch = entry.getKey();
int count = entry.getValue();
for (int i = 0; i < count; i++) {
sb.append(ch);
}
}
return sb.toString();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
String result = sortString(s);
System.out.println(result);
}
}
aW1wb3J0IGphdmEudXRpbC4qOwoKcHVibGljIGNsYXNzIE1haW4gewogICAgcHVibGljIHN0YXRpYyBTdHJpbmcgc29ydFN0cmluZyhT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KGNoKTsKICAgICAgICAgICAgfQogICAgICAgIH0KCiAgICAgICAgcmV0dXJuIHNiLnRvU3RyaW5nKCk7CiAgICB9CgogICAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgewogICAgICAgIFNjYW5uZXIgc2Nhbm5lciA9IG5ldyBTY2FubmVyKFN5c3RlbS5pbik7CiAgICAgICAgU3RyaW5nIHMgPSBzY2FubmVyLm5leHRMaW5lKCk7CiAgICAgICAgU3RyaW5nIHJlc3VsdCA9IHNvcnRTdHJpbmcocyk7CiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKHJlc3VsdCk7CiAgICB9Cn0K
Solution Python
from collections import Counter
def sort_by_frequency(s):
char_counts = Counter(s)
sorted_chars = sorted(char_counts.items(), key=lambda x: (-x[1], x[0]))
sorted_string = ''.join(char * count for char, count in sorted_chars)
return sorted_string
# Get input
s = input()
# Sort the string by character frequency
result = sort_by_frequency(s)
# Print the sorted string
print(result)
ZnJvbSBjb2xsZWN0aW9ucyBpbXBvcnQgQ291bnRlcg0KDQpkZWYgc29ydF9ieV9mcmVxdWVuY3kocyk6DQogICAgY2hhcl9jb3VudHMgPSBDb3VudGVyKHMpDQogICAgc29ydGVkX2NoYXJzID0gc29ydGVkKGNoYXJfY291bnRzLml0ZW1zKCksIGtleT1sYW1iZGEgeDogKC14WzFdLCB4WzBdKSkNCiAgICBzb3J0ZWRfc3RyaW5nID0gJycuam9pbihjaGFyICogY291bnQgZm9yIGNoYXIsIGNvdW50IGluIHNvcnRlZF9jaGFycykNCiAgICByZXR1cm4gc29ydGVkX3N0cmluZw0KDQojIEdldCBpbnB1dA0KcyA9IGlucHV0KCkNCg0KIyBTb3J0IHRoZSBzdHJpbmcgYnkgY2hhcmFjdGVyIGZyZXF1ZW5jeQ0KcmVzdWx0ID0gc29ydF9ieV9mcmVxdWVuY3kocykNCg0KIyBQcmludCB0aGUgc29ydGVkIHN0cmluZw0KcHJpbnQocmVzdWx0KQ0K
Preparation Tips for L&T Infotech Coding Test
- Understand the Fundamentals: Strong basics in data structures and algorithms are essential.
- Mock Tests: Take timed mock tests to simulate the actual test environment.
- Focus on Efficiency: Optimize your solutions for time and space complexity.
- Learn from Mistakes: Analyze and improve upon incorrect solutions during practice sessions.
Are you looking for coding assessment questions related to job placement? Click here to access coding practice sessions from moderate to challenging levels.
How to Approach the Coding Test?
- Read Questions Carefully: Understand the problem statement before diving into coding.
- Plan Your Solution: Draft a clear algorithm or pseudocode.
- Write Clean Code: Use meaningful variable names and add comments where necessary.
- Test Your Code: Check edge cases and validate your outputs.
- Optimise If Time Permits: Refactor your code for better efficiency.
Conclusion
The L&T Infotech coding test is your gateway to joining one of the most esteemed IT firms. With structured preparation, consistent practice, and a strong understanding of the basics, you can ace this challenge. Start your preparation today and move closer to achieving your career goals!
Frequently Asked Questions (FAQs)
1. What is the duration of the L&T Infotech coding test?
The test typically lasts between 60 to 90 minutes.
2. Which programming languages are allowed in the test?
You can use languages such as C, C++, Java, Python, etc.
3. Are there negative marks for incorrect answers?
Generally, there is no negative marking but confirmed during the test instructions.
4. How can freshers prepare effectively for the test?
Focus on practising coding problems, understanding algorithms, and taking mock tests.
5. Is the coding test the same for all roles?
The difficulty level and question type may vary depending on the role and job profile.
Disclaimer: While we strive for accuracy, we do not guarantee its completeness or reliability. Readers are encouraged to verify all facts and statistics from the official company website or check independently before making decisions.
Suggested reads:
- L&T Infotech Placement Process, Syllabus, & Test Pattern for 2025
- Comprehensive Guide to L&T Infotech Placement Process and Papers
- A Complete Guide to Cognizant GenC Next 2025: Pattern and Syllabus
- Cognizant GenC Exam Pattern and Syllabus: Guide for Fresher 2025
- Wipro WILP Syllabus: Detailed Syllabus Breakdown & Tips for Freshers
Instinctively, I fall for nature, music, humor, reading, writing, listening, traveling, observing, learning, unlearning, friendship, exercise, etc., all these from the cradle to the grave- that's ME! It's my irrefutable belief in the uniqueness of all. I'll vehemently defend your right to be your best while I expect the same from you!
Login to continue reading
And access exclusive content, personalized recommendations, and career-boosting opportunities.
Subscribe
to our newsletter
Blogs you need to hog!
How To Write Finance Cover Letter For Morgan Stanley (+Free Sample!)
55+ Data Structure Interview Questions For 2025 (Detailed Answers)
How To Negotiate Salary With HR: Tips And Insider Advice
Comments
Add comment