L&T Coding Questions with Solutions 2024 (NEW), Get L&T Placement Papers

L&T Coding Questions with Solutions 2024 | L&T Coding Questions | LTI Coding Questions and Answers | L&T Coding Questions and Answers Pdf 

L&T Coding Questions with Solutions 2024: The Coding Questions and Answers is very important in the IT Sector and Private Job Sector. Now days candidates are mostly interested in the off-Campus drive and private jobs. Eagerly prepare for the Coding Interview, Assessment Test, and attend the many samples online test for Private jobs. Following of the hiring, L&T issued the Off Campus Drive for eligible and capable candidates. The L&T Off Campus Drive selection process coding test, technical test etc., The Coding Interview round will be asked many questions through sample of C, C++, Java, Python Programming Languages. Thoroughly prepare for the L&T Coding Interview. Here we uploaded major common L&T Coding Questions and L&T Coding Questions with Answers. Applicants must prepare LTI Coding Questions and Answers.

You have spent a time to solve the L&T Coding Questions while using this page. The Very L&T Coding Interview questions consist of each programming languages. So, candidates prepare all programming languages on one by one. Variety of the sample L&T Coding Interview Questions makes confidence to attend the L&T Off Campus drive. Once you have selected in the LTI coding Interview round, you will be move on further selection process. The coding interview round is main part of the L&T Hiring. Candidates focused to prepare LTI Coding Questions and L&T Coding Interview Questions and Answers.

L&T Coding Questions and Answers 

Q) Johnny was absent in his english class when the vowel topic was taught by the teacher . His english teacher gave him two strings and told him to find the length of the longest common subsequence which contains only vowels, as a punishment for not attending english class yesterday .

Help Jhonny by writing a code to find the length of the longest common subsequence

Input Specification:

  • input1: First string given by his teacher
  • input2: Second string given by his teacher.

Output Specification:

  • Return the length of the longest common subsequence which contains only vowels.

Example 1:

vowelpunish

english

Output : 2

Example 2:

prepinsta

prepare

Output : 2

C++ Program 

#include <bits/stdc++.h>
using namespace std;

bool isVowel (char ch) 
{
    if (ch == 'a' || ch == 'e' || ch == 'i' ||ch == 'o' || ch == 'u')
        return true;
    return false;
}
 
int lcs (char *X, char *Y, int m, int n) 
{
    int L[m + 1][n + 1];
    int i, j;
    L[i][j] contains length of LCS of X[0..i - 1] and Y[0..j - 1] 
    for (i = 0; i <= m; i++)
    {
        for (j = 0; j <= n; j++)
	    {
            if (i == 0 || j == 0)
                L[i][j] = 0;
        	else if ((X[i - 1] == Y[j - 1]) && isVowel (X[i - 1]))
                L[i][j] = L[i - 1][j - 1] + 1;
        	else
                L[i][j] = max (L[i - 1][j], L[i][j - 1]);
        }
    }
    return L[m][n];
}
 
int main () 
{
    char X[];
    char Y[];
    
    cin >> X;
    cin >> Y;
    
    int m = strlen (X);
    int n = strlen (Y);
    
    cout << lcs (X, Y, m, n);
    return 0;
}

JAVA PROGRAM
import java.util.*;
public class Main
{

    static boolean Vowel(char ch)
    {
    	if (ch == 'a' || ch == 'e' ||ch == 'i' || ch == 'o' ||ch == 'u')
		    return true;
	    return false;
    }

    static int Lcs(String X, String Y, int m, int n)
    {
	    int L[][] = new int[m + 1][n + 1];
	    int i, j;

    	for (i = 0; i <= m; i++)
	    {
		    for (j = 0; j <= n; j++)
		    {
			    if (i == 0 || j == 0)
				    L[i][j] = 0;
    			else if ((X.charAt(i - 1) == Y.charAt(j - 1)) &&Vowel(X.charAt(i - 1)))
			    	L[i][j] = L[i - 1][j - 1] + 1;
    			else
		    		L[i][j] = Math.max(L[i - 1][j],L[i][j - 1]);
		    }
    	}
    	return L[m][n];
    }

    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
	    String a = sc.next();
	    String b = sc.next();
        int i = a.length();
	    int j = b.length();
    	System.out.println(""+Lcs(a, b, i, j));
    }
}

PHYTHON PROGRAM 
a=input()

b= input()

m,n=len(a),len(b)

vow="aeiou"

DP= [[0 for i in range(n + 1)]for j in range(m + 1)]

i, j = 0, 0

for i in range(m + 1):

    for j in range(n + 1):

        if (i == 0 or j == 0):

            DP[i][j] = 0

        elif ((a[i - 1] == b[j - 1]) and a[i - 1] in vow):

            DP[i][j] = DP[i - 1][j - 1] + 1

        else:

            DP[i][j] = max(DP[i - 1][j],DP[i][j - 1])

print(DP[m][n])

Q) Maxwell filled his university exam form in which he has filled his mid sem marks but by mistake he has filled DS marks in AI field and AI marks in DS field. He wants to update his mid sem marks but for that he has to pay some penalty.

Penalty equal to the sum of absolute differences between adjacent subject marks.

Return the minimum penalty that must be paid by him.

Input Specification:

  • input1: length of an integer array of numbers (2 <= input 1 <= 1000)
  • input2: integer array(1 <= input2[i] <= 10000)

Example 1:

Input : arr = {4, 1, 5}

Output : 5

Explanation: Sum of absolute differences is |4-5| + |1-4| + |5-4|

Example 2:

Input : arr = {5, 10, 1, 4, 8, 7}

Output : 9

Example 3:

Input : {12, 10, 15, 22, 21, 20, 1, 8, 9}

Output : 18

C++ Program 
#include <bits/stdc++.h>
using namespace std;

int sumOfMinAbsDifferences(int arr[], int n)
{
	sort(arr, arr+n);
	int sum = 0;
	
	sum += abs(arr[0] - arr[1]);
	sum += abs(arr[n-1] - arr[n-2]);
	
	for (int i=1; i<n-1; i++)
		sum += min(abs(arr[i] - arr[i-1]), abs(arr[i] - arr[i+1]));
		
	// required sum
	return sum;
}

// Driver program to test above
int main()
{
	int n;
        cin>>n;
	int arr[n];
	for(int i=0 ; i<n ; i++)
		cin>>arr[i];
	
	cout << "Sum = "<< sumOfMinAbsDifferences(arr, n);
}

Java Program 

import java.util.*;

public class Main {

	static int Penalty(int arr[] ,int n)
	{
		Arrays.sort(arr);
		int sum = 0;

		sum += Math.abs(arr[0] - arr[1]);
		sum += Math.abs(arr[n-1] - arr[n-2]);
		
		for (int i = 1; i < n - 1; i++)
			sum +=
			Math.min(Math.abs(arr[i] - arr[i-1]),
					Math.abs(arr[i] - arr[i+1]));
		return sum;
	}	

	
	public static void main(String args[])
	{
	    Scanner sc = new Scanner(System.in);
	    int n = sc.nextInt();
	    int arr[] = new int[n];
	    for(int i=0 ; i<n ; i++)
	        arr[i] = sc.nextInt();

		System.out.println(""+ Penalty(arr, n));
	}
}

Python Program 
n=int(input())

arr = sorted(list(map(int,input().split())))

s=abs(arr[0]-arr[1])+abs(arr[-1]-arr[-2])

for i in range(1,n-1):

    s+=min(abs(arr[i]-arr[i-1]),abs(arr[i]-arr[i+1]))

print(s)

Q) Given an array of integers (both odd and even), the task is to arrange them in such a way that odd and even values come in alternate fashion in non-decreasing order(ascending) respectively. 

  • If the smallest value is Even then we have to print Even-Odd pattern.
  • If the smallest value is Odd then we have to print Odd-Even pattern.

Note: No. of odd elements must be equal to No. of even elements in the input array.

Examples:

Input:  arr[] = {1, 3, 2, 5, 4, 7, 10}

Output: 1, 2, 3, 4, 5, 10, 7

Smallest value is 1(Odd) so output will be Odd-Even pattern.

Input: arr[] = {9, 8, 13, 2, 19, 14}

Output: 2, 9, 8, 13, 14, 19

Smallest value is 2(Even) so output will be Even-Odd pattern.

C++ Program 

#include <bits/stdc++.h>
using namespace std;

void AlternateRearrange(int arr[], int n)
{
	// Sort the array
	sort(arr, arr + n);

	vector<int> v1; // to insert even values
	vector<int> v2; // to insert odd values

	for (int i = 0; i < n; i++)
		if (arr[i] % 2 == 0)
			v1.push_back(arr[i]);
		else
			v2.push_back(arr[i]);

	int index = 0, i = 0, j = 0;

	bool flag = false;

	// Set flag to true if first element is even
	if (arr[0] % 2 == 0)
		flag = true;

	// Start rearranging array
	while (index < n) {

		// If first element is even
		if (flag == true) {
			arr[index++] = v1[i++];
			flag = !flag;
		}

		// Else, first element is Odd
		else {
			arr[index++] = v2[j++];
			flag = !flag;
		}
	}

	// Print the rearranged array
	for (i = 0; i < n; i++)
		cout << arr[i] << " ";
}

// Driver code
int main()
{
	int arr[] = { 9, 8, 13, 2, 19, 14 };
	int n = sizeof(arr) / sizeof(int);
	AlternateRearrange(arr, n);
	return 0;
}
Java Program 
import java.util.* ;

class Main
{

	static void AlternateRearrange(int arr[], int n)
	{
		// Sort the array
		
		// Collection.sort() sorts the
		// collection in ascending order
		Arrays.sort(arr) ;
		
		Vector v1 = new Vector(); // to insert even values
		Vector v2 = new Vector(); // to insert odd values
	
		for (int i = 0; i < n; i++)
			if (arr[i] % 2 == 0)
				v1.add(arr[i]);
			else
				v2.add(arr[i]);
	
		int index = 0, i = 0, j = 0;
	
		boolean flag = false;
	
		// Set flag to true if first element is even
		if (arr[0] % 2 == 0)
			flag = true;
	
		// Start rearranging array
		while (index < n)
		{
	
			// If first element is even
			if (flag == true)
			{
				arr[index] = (int)v1.get(i);
				i += 1 ;
				index += 1 ;
				flag = !flag;
			}
	
			// Else, first element is Odd
			else
			{
				arr[index] = (int)v2.get(j) ;
				j += 1 ;
				index += 1 ;
				flag = !flag;
			}
		}
	
		// Print the rearranged array
		for (i = 0; i < n; i++)
			System.out.print(arr[i] + " ");
	}
	
	// Driver code
	public static void main(String []args)
	{
		int arr[] = { 9, 8, 13, 2, 19, 14 };
		int n = arr.length ;
	
		AlternateRearrange(arr, n);
	}
}

Q) Given a string s, the task is to find out the minimum number of adjacent swaps required to make a string is palindrome. If it is not possible, then return -1.

Examples:
Input: aabcb
Output: 3

Explanation:
After 1st swap: abacb
After 2nd swap: abcab
After 3rd swap: abcba

Input: adbcdbad
Output: -1

C++ Program 

#include <bits/stdc++.h>
using namespace std;

// Function to Count minimum swap
int countSwap(string s)
{
	// calculate length of string as n
	int n = s.length();

	// counter to count minimum swap
	int count = 0;

	// A loop which run till mid of
	// string
	for (int i = 0; i < n / 2; i++) {
		// Left pointer
		int left = i;

		// Right pointer
		int right = n - left - 1;

		// A loop which run from right
		// pointer towards left pointer
		while (left < right) {
			// if both char same then
			// break the loop.
			// If not, then we have to
			// move right pointer to one
			// position left
			if (s[left] == s[right]) {
				break;
			}
			else {
				right--;
			}
		}

		// If both pointers are at same
		// position, it denotes that we
		// don't have sufficient characters
		// to make palindrome string
		if (left == right) {
			return -1;
		}

		// else swap and increase the count
		for (int j = right; j < n - left - 1; j++) {
			swap(s[j], s[j + 1]);
			count++;
		}
	}

	return count;
}

// Driver code
int main()
{
	string s;
  	cin>>s;

	// Function calling
	int ans1 = countSwap(s);

	reverse(s.begin(), s.end());
	int ans2 = countSwap(s);

	cout << max(ans1, ans2);

	return 0;
}
 Java Program 
import java.util.*;

public class Main {

	// Function to Count minimum swap
	static int countSwap(String str)
	{

		// Legth of string
		int n = str.length();

		// it will convert string to
		// char array
		char s[] = str.toCharArray();

		// Counter to count minimum
		// swap
		int count = 0;

		// A loop which run in half
		// string from starting
		for (int i = 0; i < n / 2; i++) {

			// Left pointer
			int left = i;

			// Right pointer
			int right = n - left - 1;

			// A loop which run from
			// right pointer to left
			// pointer
			while (left < right) {

				// if both char same
				// then break the loop
				// if not same then we
				// have to move right
				// pointer to one step
				// left
				if (s[left] == s[right]) {
					break;
				}
				else {
					right--;
				}
			}

			// it denotes both pointer at
			// same position and we don't
			// have sufficient char to make
			// palindrome string
			if (left == right) {
				return -1;
			}
			else if (s[left] != s[n - left - 1]) {
				char temp = s[right];
				s[right] = s[n - left - 1];
				s[n - left - 1] = temp;
				count++;
			}
		}

		return count;
	}

	// Driver Code
	public static void main(String[] args)
	{
                Scanner sc = new Scanner(System.in)
		String s = sc.next();

		// Function calling
		int ans1 = countSwap(s);
		System.out.println(ans1);
	}
}

Q) Encryption is needed to be done in important chats between army officers so that no one else can read it .
Encryption of message is done by replacing each letter with the letter at 3 positions to the left
e.g. ‘a’ is replaced with ‘x. ‘b’ with ‘y’ … ‘d’ with ‘a’ and so on.

Given a encrypted input string find the corresponding plaintext and return the plaintext as output string.

Note:- All the characters are in the lower case for input and output strings

Input Specification
input1: the ciphertext

Output Specification
Return the corresponding plaintext.

Example1:
input 1: ABCDEFGHIJKLMNOPQRSTUVWXYZ
output: XYZABCDEFGHIJKLMNOPQRSTUVW

Example2:
Input 1: ATTACKATONCE
output: EXXEGOEXSRGI

C++ Program
#include <iostream>
using namespace std;

string encrypt(string text, int s)
{
	string result = "";

	// traverse text
	for (int i=0;i<text.length();i++)
	{
		if (isupper(text[i]))
		result += char(int(text[i]+s-65)%26 +65);

	
	else
		result += char(int(text[i]+s-97)%26 +97);
	}

	return result;
}

int main()
{
	string text;
	cin>>text;
	int s = 3;
	cout << encrypt(text, s);
	return 0;
}
Java Program 
import java.util.*;
public class CaesarCipher
{

	public static StringBuffer encrypt(String text, int s)
	{
		StringBuffer result= new StringBuffer();

		for (int i=0; i<text.length(); i++)
		{
			if (Character.isUpperCase(text.charAt(i)))
			{
				char ch = (char)(((int)text.charAt(i) +s - 65) % 26 + 65);
				result.append(ch);
			}
			else
			{
				char ch = (char)(((int)text.charAt(i) +s - 97) % 26 + 97);
				result.append(ch);
			}
		}
		return result;
	}


	public static void main(String[] args)
	{	Scanner sc = new Scanner(System.in)
		String text = sc.next();
		int s = 3;
		
		System.out.println("" + encrypt(text, s));
	}
}
JOB ALERT ON INSTAGRAM FOLLOW NOW>>
JOB ALERT ON YOUR EMAIL DAILY SUBSCRIBE NOW>>

While using this page to get an L&T Off Campus Coding Questions with Answers and LTI Coding Interview Coding Questions. Kindly watch Dailyrecruitment.in site.

Govt Jobs by Qualifications

Education & Vacancies Salary Apply Link
10th Pass Govt Jobs - 5,000 Vacancies Rs. 5,200 - 63,200 Apply Now
12th Pass Govt Jobs - 18,000+ Vacancies Rs. 5,200 - 92,300 Apply Now
ITI Pass Jobs - 3,500 Vacancies Rs. 5,200 - 35,000 Apply Now
Any Graduate Jobs - 19,100 Vacancies Rs. 5,200 - 92,300 Apply Now
Central Govt Jobs Rs. 5,200 - 17,000 Apply Now
Bank Jobs - 1,000 Vacancies Rs. 5,200 - 29,200 Apply Now
Diploma Jobs - 9,300 Vacancies Rs. 5,200 - 35,000 Apply Now
BTech/BE Jobs - 18,000 Vacancies Rs. 15,000 - 1,00,000 Apply Now
Data Entry Jobs - 1,300 Vacancies Rs. 5,200 - 29,200 Apply Now
Private Jobs Rs. 10,000 - 67,700 Apply Now