Incedo Coding Questions and Answers
Sample Incedo Coding Questions with Solutions
On this page, you will get Sample Incedo Coding Questions and Answers asked in Technical Interview involved Incedo Recruitment Process.
Apart from this, at the end of this you will get Faq’s related to Job profiles, Salary Offered, and specific steps of the Recruitment Process of Incedo to hire freshers.
Sample Incedo Coding Questions and Answers - Set 1
Question 1 : Guess the word
Problem Statement :
Kochouseph Chittilappilly went to Dhruv Zplanet , a gaming space, with his friends and played a game called “Guess the Word”.
Rules of games are –
Computer displays some strings on the screen and the player should pick one string / word if this word matches with the random word that the computer picks then the player is declared as Winner.
Kochouseph Chittilappilly’s friends played the game and no one won the game. This is Kochouseph Chittilappilly’s turn to play and he decided to must win the game.
What he observed from his friend’s game is that the computer is picking up the string whose length is odd and also that should be maximum. Due to system failure computers sometimes cannot generate odd length words. In such cases you will lose the game anyways and it displays “better luck next time”. He needs your help. Check below cases for better understand
Sample input 0:
5 → number of strings
Hello Good morning Welcome you
Sample output 0:
morning
Explanation:
Hello → 5
Good → 4
Morning → 7
Welcome → 7
You → 3
First word that is picked by computer is morning
Sample input 1:
3
Go to hell
Sample output 1:
Better luck next time
Explanation:
Here no word with odd length so computer confuses and gives better luck next time
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string a, result = "";
while (n--) {
cin >> a;
if (a.length() & 1)
if (a.length() > result.length())
result = a;
}
if (result == "") cout << "Better luck next time";
else cout << result;
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String arr[] = new String[n];
for (int i = 0; i < n; i++)
arr[i] = sc.next();
int len = 0;
ArrayList < String > oddLength = new ArrayList < String > ();
for (int i = 0; i < n; i++) { len = arr[i].length(); if (len % 2 == 1) oddLength.add(arr[i]); } if (oddLength.size() == 0) System.out.println("Better luck next time"); else { Iterator itr = oddLength.iterator(); int max = -1; String res = ""; while (itr.hasNext()) { String temp = (String) itr.next(); if (temp.length() > max) {
res = temp;
max = temp.length();
}
}
System.out.println(res);
}
}
}
n = int(input())
L = list(map(str, input().split()))
result = ""
for a in L:
if len(a) & 1:
if len(a) > len(result):
result = a
if result == "":
result = "Better luck next time"
print(result)
Question 02: Array Subarray
Problem Statement: You are given an array, You have to choose a contiguous subarray of length ‘k’, and find the minimum of that segment, return the maximum of those minimums.
Sample input :
- 1 → Length of segment x =1
- 5 → size of space n = 5
- 1 → space = [ 1,2,3,1,2]
- 2
- 3
- 1
- 2
Sample output :
- 3
Explanation :
- The subarrays of size x = 1 are [1],[2],[3],[1], and [2],Because each subarray only contains 1 element, each value is minimal with respect to the subarray it is in. The maximum of these values is 3. Therefore, the answer is 3
#include <bits/stdc++.h>
using namespace std;
vector < int > arr;
int prevmin=-1;
int flag=0;
int x,n,q;
int sorting(int start,int end)
{
if(start+1==n) {start=0;end=end-n;}
if(start==end) return arr[start];
return min(arr[start],sorting(start+1,end));
}
int func(int start,int end)
{
if(flag==0) {flag++;return prevmin=sorting(start,end);}
if(arr[start-1]==prevmin) return prevmin;
return prevmin=(arr[end] <= prevmin)?prevmin:sorting(start,end); } int main() { cin >> x >> n;
int ans=0;
for(int i=0;i < n;i++) {cin >> q;arr.push_back(q);}
for(int i=0;i < n;i++)
{
ans=max(ans,func(i,i+x-1));
}
cout << ans;
}
import java.util.*;
public class DiskSpace
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
int n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i < n;i++)
arr[i]=sc.nextInt();
int min=Integer.MAX_VALUE;
int max=Integer.MIN_VALUE;
for(int i=0;i <= n-x;i++)
{
min=Integer.MAX_VALUE;
for(int j=i;j < (i+x);j++)
min=Math.min(min,arr[j]);
max=Math.max(min,max);
}
System.out.println(max);
}
}
n=int(input())
arr=[]
for i in range(n):
arr.append(int(input()))
s,a=0,0
for i in arr:
s=s+i
if(s < 1):
a=a+(-1*s)
s=0
print(a)
Question 3: Good Prime Number
Problem Statement :
A prime number is a number which is divisible by one and itself. Also a number is called a good prime number if the sum of its digits is a prime number. For example a number 23 is a good prime number because the sum of 2 and 3 ( 2+3=5) is 5 which is a prime number. You are given an integer K. Your task is to find the kth good prime number that is greater than a provided number N.
For example , 232 is a good prime number since the sum of all digits is 7 which is a prime number whereas 235 is not a good prime number.
Input format :
- The first line contains an integer N.
- The next line contains an integer K.
Output format :
A single integer which is a Kth good prime number that is greater than a provided number N.
Constraints :
- 1<=N<=10^5
- 1<=K<<=10^5
Sample Input 1:
4 4
Sample Output 1:
12
Explanation :
Good prime numbers starting from 4 are 5,7,11(1+1=2 which is prime number),12(1+2=3 which is prime number),14(1+4=5 which is a prime number) and so on. Because the sum of digits of an individual number is a prime number And 4 th good prime number is 12 in this series.Hence the output is 12.
Sample Input 2:
17 5
Sample Output 2:
29
Explanation :
Good prime numbers starting from 17 are 20,21,23,25,29…and the 5th prime number is 29.Hence the output is 29.
#include<bits/stdc++.h>
using namespace std;
bool isPrime(int n) {
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if ((n % i == 0) || (n % (i + 2) == 0))
return false;
return true;
}
int solve(int n, int k) {
int c = 0;
vector < int > list;
for (int i = n + 1; c < k; i++) { int temp = i; int sum = 0; while (temp != 0) { sum = sum + temp % 10; temp = temp / 10; } if (isPrime(sum)) { list.push_back(i); c++; } } return list[k - 1]; } int main() { int n; cin >> n;
int k;
cin >> k;
cout << solve(n, k);
return 0;
}
import java.util.*;
class Main {
public static boolean isPrime(int n) {
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if ((n % i == 0) || (n % (i + 2) == 0))
return false;
return true;
}
public static int solve(int n, int k) {
int c = 0;
ArrayList < Integer > list = new ArrayList < > ();
for (int i = n + 1; c < k; i++) {
int temp = i;
int sum = 0;
while (temp != 0) {
sum = sum + temp % 10;
temp = temp / 10;
}
if (isPrime(sum)) {
list.add(i);
c++;
}
}
return list.get(k - 1);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
System.out.println(solve(n, k));
}
}
import math
def isPrime(n):
if n <= 1:
return False
if n <= 3:
return True
if n % 2 == 0 or n % 3 == 0:
return False
for i in range(5, int(math.sqrt(n)) + 1, 6):
if n % i == 0 or n % (i + 2) == 0:
return False
return True
def solve(n, k):
c = 0
list = []
for i in range(n + 1, 1000000000):
temp = i
sum = 0
while temp != 0:
sum = sum + temp % 10
temp = temp // 10
if isPrime(sum):
list.append(i)
c += 1
if c == k:
break
return list[k - 1]
n, k = map(int, input().split())
print(solve(n, k))
Question 4 : Simple problem
Problem Statement :
Mr X is a teacher of maths. He came across a very simple problem. In the problem you have to arrange the numbers in an ascending order and calculate the total number of swaps required. The number of swaps must be minimum. But Mr X is busy with some other tasks and you being his favourite student , so he asks you to solve this problem.
Constraints:
1<=T<=100
1<=N<=100
1<=A[ ] <=1000
Examples
Input :
4
4 3 1 2
Output:
2
Explanation: Swap index 0 with 3 and 1 with 2 to form the sorted array {1, 2, 3, 4}.
Input :
5
1 5 4 3 2
Output :
2
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector < int > arr(n);
for (int i = 0; i < n; i++) cin >> arr[i];
vector < pair < int, int >> ap(n);
for (int i = 0; i < n; i++) {
ap[i].first = arr[i];
ap[i].second = i;
}
sort(ap.begin(), ap.end());
vector < bool > v(n, false);
int ans = 0;
for (int i = 0; i < n; i++) { if (v[i] || ap[i].second == 1) continue; int cs = 0, j = i; while (!v[j]) { v[j] = 1; j = ap[j].second; cs++; } if (cs > 0) ans += cs - 1;
}
cout << ans;
}
import java.util.Scanner;
public class Main {
static int minimumSwaps(int[] arr) {
int count = 0;
int i = 0;
while (i < arr.length) {
if (arr[i] != i + 1) {
while (arr[i] != i + 1) {
int temp = 0;
temp = arr[arr[i] - 1];
arr[arr[i] - 1] = arr[i];
arr[i] = temp;
count++;
}
}
i++;
}
return count;
}
public static void main(String[] args) {
Scanner ss = new Scanner(System.in);
int n = ss.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = ss.nextInt();
}
System.out.println(minimumSwaps(arr));
}
}
t = int(input())
a = []
m = -1
m1 = -1
for i in range(t):
m1 = int(input())
a.append(m1)
m = max(m, m1)
k2 = 2
n = m + 1
k1 = ["0"] * (n)
k1[1] = "1"
a1 = 1
while k2 < (n):
if k1[a1][-1] == "0":
k1[k2] = k1[a1] + "0"
k2 += 1
if k2 >= (n):
break
k1[k2] = k1[a1] + "1"
k2 += 1
if k2 >= (n):
break
a1 += 1
elif k1[a1][-1] == "1":
k1[k2] = k1[a1] + "0"
k2 += 1
if k2 >= (n):
break
a1 += 1
for i in a:
print(k1[i])
Question 5 : Seating Arrangement in Exam Hall
Problem Statement :
Semester exams are going on for university students. Examiners noticed that a group of people are trying to cheat. They marked students of that group as ‘1’ and students of another group ( who are not cheating ) as ‘0’
We can reduce cheating by not allowing students from group 1 to sit together, means no two students from group 1 can sit together. Seatings are marked using above conditions. Your task is to give the seating placement of nth possibility Possibility order from 1 to 10 is given below
[1 10 100 101 1000 1001 1010 10000 10001 10010]
Sample input :
3 → number of test cases
4
6
9
Sample output :
101
1001
10001
Explanation :
4th possibility is 101
6th possibility is 1001
9th possibility is 10001
#include<bits/stdc++.h>
using namespace std;
int main() {
int n, m, Max = 0;
cin >> n;
vector < int > v(n);
vector < string > arr;
for (int i = 0; i < n; i++) { cin >> v[i];
Max = max(Max, v[i]);
}
queue < string > q;
q.push("1");
int i = 1;
arr.push_back("1");
while (!q.empty()) { //cout<<"TEST"<<endl; string a = q.front(); q.pop(); q.push(a + "0"); arr.push_back(a + "0"); i++; if (a[a.length() - 1] == '0') { q.push(a + "1"); arr.push_back(a + "1"); i++; } if (i > Max) break;
}
for (int i = 0; i < n; i++) {
cout << arr[v[i] - 1] << endl;
}
}
import java.util.*;
class Main {
public static void possibilities(int n) {
int c = 0;
String b = "";
for (int i = 1; n != c; i++) {
String s = Integer.toString(i, 2);
if (!s.contains("11")) {
c++;
b = s;
}
}
System.out.println(b);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int tc = sc.nextInt();
int[] a = new int[tc];
for (int i = 0; i < tc; i++) {
a[i] = sc.nextInt();
}
for (int i = 0; i < tc; i++) {
possibilities(a[i]);
}
}
}
t = int(input())
a = []
m = -1
m1 = -1
for i in range(t):
m1 = int(input())
a.append(m1)
m = max(m, m1)
k2 = 2
n = m + 1
k1 = ["0"] * (n)
k1[1] = "1"
a1 = 1
while k2 < (n): if k1[a1][-1] == "0": k1[k2] = k1[a1] + "0" k2 += 1 if k2 >= (n):
break
k1[k2] = k1[a1] + "1"
k2 += 1
if k2 >= (n):
break
a1 += 1
elif k1[a1][-1] == "1":
k1[k2] = k1[a1] + "0"
k2 += 1
if k2 >= (n):
break
a1 += 1
for i in a:
print(k1[i])
Sample Incedo Coding Questions and Answers - Set 2
Question 1 : Game Of Clicks [ R->Hard ]
Problem Statement :
Sahil watches TV all day and gets bored. He started playing this dumb game of identifying minimum number of inputs needed to reach a channel. As his cousin, you have to help him, but you live far from his house. So you decide to write a code that will ask Sahil for some inputs and give outputs respectively.
Here are the problems you need to keep in mind :
- There are 13 buttons on his remote: 10 buttons for the numbers (0-9) to form integers denoting respective channel index, “Up channel” button and “ Down channel” button for going i +1th channel and i-1th channel from i respectively, and a “Last viewed” button to see what’s the last channel before it.
- The number buttons allow you to jump directly to a specific channel (Ex: to go to channel 172 by typing 1,7,2).
- If the channel which you are in is ith and that is the max channel index possible, by Up channel, you will reach the first channel possible. Same goes for the down channel button. You can go to the highest channel possible if you go down from the lowest channel possible.
- Sahil can get from one channel to the next in one of the two ways.
- Sahil’s parents have set some parental control on some channels on Aniruth’s television. The “Up Channel “ and “Down buttons” buttons skip these channels as these channels are not viewable.
- Given a list of channels to view, the lowest channel, the highest channel, and a list of blocked channels, your program should return the minimum number of clicks necessary to get through all the shows that Anirudh would like to match.
Input Format :
- First line is the lowest Channel
- Second-line is the highest Channel
- Followed by a number of blocked channels B,
and the next B lines contain the actual blocked channels. - Followed by the number of Channels to view V, and the next V lines contain the actual channels to view.
Constraints :
- The lowest channel on the television will be greater than 0. and less than or equal to 10,000.
- The highest channel on the television will be greater than or equal to the lowest channel. and less than or equal to 10.000.
- The list of channels that are blocked on Anirudh’s television. All the channels in this list will be valid channels (greater than or equal to lowest channel, less than or equal 1 to highest channel). Duplicates may be Ignored. The blocked list can be a maximum of 40 channels.
- The sequence that Sahil must view contains between 1 and 50 elements. inclusive. All channels in this sequence are not in the blocked list and are between lowest channel and highest channel. Inclusive.
Sample Input 0:
1
20
2
18
19
5
15
14
17
11
17
Sample output 0:
8
#include<bits/stdc++.h>
using namespace std;
unordered_map < int, int > m;
int l, u;
int util(int a, int b) {
if (a == b) return 0;
if (m[a]) return util(a + 1, b);
return 1 + util(a + 1, b);
}
int func(int b, int prev) {
if (b < prev) return min(util(prev, u) + util(l, b) + 1, util(b, prev)); else return min(util(prev, b), util(l, b) + util(prev, u) + 1); } int main() { int flag = 0, ans = 0, prev, prev2; cin >> l >> u;
int bn, b;
cin >> bn;
while (bn--) {
cin >> b;
m[b]++;
}
cin >> bn;
while (bn--) {
cin >> b;
if (b > 9 && flag == 0) {
ans += 2;
flag++;
prev = b;
} else if (flag == 0) {
ans += 1;
flag++;
prev = b;
} else if (prev2 == b) {
prev2 = prev;
prev = b;
ans++;
} else {
ans += min(b > 9 ? 2 : 1, func(prev, b));
prev2 = prev;
prev = b;
}
}
cout << ans;
}
def prev(now, l, h, blocked):
if now != l:
if (now - 1) not in blocked:
return now - 1
else:
return prev(now - 1, l, h, blocked)
else:
if h not in blocked:
return h
else:
return prev(h, l, h, blocked)
def next(now, l, h, blocked):
if now != h:
if (now + 1) not in blocked:
return now + 1
else:
return next(now + 1, l, h, blocked)
else:
if l not in blocked:
return l
else:
return next(l, l, h, blocked)
def digits(n):
count = 0
while n > 0:
n = n // 10
count += 1
return count
for i in range(2):
if i == 0:
l = int(input())
else:
h = int(input())
b = int(input())
blocked = []
for i in range(b):
blocked.append(int(input()))
back = -1
now = -1
c = int(input())
k = 0
for i in range(c):
n = int(input())
n1 = digits(n)
if now == -1:
now = n
k += n1
continue
if back == n:
k += 1
back, now = now, back
continue
pf = 0
pb = 0
now1 = now
prev1 = now
for j in range(n1):
if j == (n1 - 1):
pf = n1
pb = n1
break
else:
now1 = next(now1, l, h, blocked)
pf += 1
prev1 = prev(prev1, l, h, blocked)
pb += 1
if now1 == n:
break
if prev1 == n:
break
k += pf
back = now
now = n
print(k)
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int l = sc.nextInt();
int h = sc.nextInt();
int b = sc.nextInt();
List blackList = new ArrayList<>();
for (int i = 0; i < b; i++) {
blackList.add(sc.nextInt());
}
int v = sc.nextInt();
Set validList = new HashSet<>();
for (int i = 0; i < v; i++) {
int value = sc.nextInt();
if (!blackList.contains(value)) {
validList.add(value);
}
}
int totalLength = 0;
for (int value : validList) {
totalLength += String.valueOf(value).length();
}
System.out.println(totalLength);
}
}
Question 2: Consecutive Prime Sum
Problem Description
Question -: Some prime numbers can be expressed as a sum of other consecutive prime numbers.
- For example
- 5 = 2 + 3,
- 17 = 2 + 3 + 5 + 7,
- 41 = 2 + 3 + 5 + 7 + 11 + 13.
Your task is to find out how many prime numbers which satisfy this property are present in the range 3 to N subject to a constraint that summation should always start with number 2.
Write code to find out the number of prime numbers that satisfy the above-mentioned property in a given range.
Input Format: First line contains a number N
Output Format: Print the total number of all such prime numbers which are less than or equal to N.
Constraints: 2<N<=12,000,000,000
#include<bits/stdc++.h>
using namespace std;
int prime(int b)
{
int j,cnt;
cnt=1;
for(j=2;j<=b/2;j++)
{
if(b%j==0)
cnt=0;
}
if(cnt==0)
return 1;
else
return 0;
}
int main()
{
int i,j,n,cnt,a[25],c,sum=0,count=0,k=0;
cout<<"Enter a number : ";
cin>>n;
for(i=2;i<=n;i++)
{
cnt=1;
for(j=2;j<=n/2;j++)
{
if(i%j==0)
cnt=0;
}
if(cnt==1)
{
a[k]=i;
k++;
}
}
for(i=0;i<k;i++)
{
sum=sum+a[i];
c= prime(sum);
if(c==1)
count++;
}
cout<<count;
return 0;
}
Output Enter a number : 5 1
import java.util.Scanner; class Main { static int prime(int b) { int j,cnt; cnt=1; for (j = 2; j <= b/2; j++) { if(b%j==0) cnt=0; } if(cnt==0) return 1; else return 0; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int i,j,n=0,cnt,c=0,sum=0,count=0,k=0; Main t = new Main(); int[] a = new int[25]; System.out.println("Enter no"); n = sc.nextInt(); for (i = 2; i <=n ; i++) { cnt=1; for (j = 2; j <= n/2; j++) { if(i%j==0) cnt=0; } if(cnt==1) { a[k]=i; k++; } } for (i = 0; i < k; i++) { sum=sum+a[i]; c=t.prime(sum); if(c==1) count++; } System.out.println(count); } }
Output 43 4
num = int(input()) arr = [] sum = 0 count = 0 if num > 1: for i in range(2, num + 2): for j in range(2, i): if i % j == 0: break else: arr.append(i) def is_prime(sum): for i in range(2, (sum // 2) +2): if sum % i == 0: return False else: return True for i in range(0, len(arr)): sum = sum + arr[i] if sum <= num: if is_prime(sum): count = count + 1 print(count)
Output 20 2
Question 3: Majority Element
The majority element in an array is defined as the element that appears more than ⌊n/2⌋ times, where n is the length of the array.
In other words, it is the element that occurs most frequently and makes up more than half of the array.
Given an array of integers, the task is to find the majority element and return it. If there is no majority element, If there is no majority element, the algorithm should indicate that.
Examples:
Example 1:
Input: [3, 3, 4, 2, 4, 4, 2, 4, 4]
Output: 4
Explanation:
In the given array, the number 4 appears 5 times, which is more than half of the array size (9/2 = 4.5). Therefore, 4 is the majority element.
Example 2:
Input: [1, 2, 3, 4, 4, 4, 4]
Output: 4
Explanation:
In this case, the number 4 appears 4 times, which is more than half of the array size (7/2 = 3.5). Thus, 4 is the majority element.
Example 3:
Input: [1, 2, 3, 4, 5]
Output: -1
Explanation:
There is no majority element in this array since no number appears more than half of the array size (5/2 = 2.5).
Example 4:
Input: [2, 2, 2, 3, 3, 4, 4, 4, 4]
Output: -1
Explanation:
In this case, although the number 4 appears 4 times, it does not occur more than half of the array size (9/2 = 4.5).
Hence, there is no majority element.
#include<bits/stdc++.h>
int majorityElement(std::vector& nums) {
int count = 0;
int candidate = 0;
for (int num : nums) {
if (count == 0) {
candidate = num;
count = 1;
} else if (num == candidate) {
count++;
} else {
count--;
}
}
return candidate;
}
int validateMajorityElement(std::vector& nums, int candidate) {
int count = 0;
for (int num : nums) {
if (num == candidate) {
count++;
}
}
if (count > nums.size() / 2) {
return candidate;
} else {
return -1; // No majority element found
}
}
int findMajorityElement(std::vector& nums) {
int candidate = majorityElement(nums);
return validateMajorityElement(nums, candidate);
}
int main() {
std::vector nums = {3, 3, 4, 2, 4, 4, 2, 4, 4};
int result = findMajorityElement(nums);
if (result != -1) {
std::cout << "Majority Element: " << result << std::endl;
} else {
std::cout << "No majority element found." << std::endl;
}
return 0;
}
def majority_element(nums):
count = 0
candidate = None
for num in nums:
if count == 0:
candidate = num
count = 1
elif num == candidate:
count += 1
else:
count -= 1
return candidate
def validate_majority_element(nums, candidate):
count = 0
for num in nums:
if num == candidate:
count += 1
if count > len(nums) // 2:
return candidate
else:
return None
def find_majority_element(nums):
candidate = majority_element(nums)
return validate_majority_element(nums, candidate)
# Example usage
nums = [3, 3, 4, 2, 4, 4, 2, 4, 9]
result = find_majority_element(nums)
if result is not None:
print("Majority Element:", result)
else:
print("No majority element found.")
import java.util.HashMap;
import java.util.Map;
public class Main {
public static int majorityElement(int[] nums) {
int count = 0;
int candidate = 0;
for (int num : nums) {
if (count == 0) {
candidate = num;
count = 1;
} else if (num == candidate) {
count++;
} else {
count--;
}
}
return candidate;
}
public static int validateMajorityElement(int[] nums, int candidate) {
int count = 0;
for (int num : nums) {
if (num == candidate) {
count++;
}
}
if (count > nums.length / 2) {
return candidate;
} else {
return -1; // No majority element found
}
}
public static int findMajorityElement(int[] nums) {
int candidate = majorityElement(nums);
return validateMajorityElement(nums, candidate);
}
public static void main(String[] args) {
int[] nums = {3, 3, 4, 2, 4, 4, 2, 4, 4};
int result = findMajorityElement(nums);
if (result != -1) {
System.out.println("Majority Element: " + result);
} else {
System.out.println("No majority element found.");
}
}
}
Question 4: Smallest window in a string containing all the characters of another string
Given two strings S and P, the task is to find the smallest window in string S that contains all the characters (including duplicates) of string P. If no such window exists, return “-1”. If there are multiple windows of the same length, return the one with the smallest starting index.
Note that all characters are lowercase alphabets.
Example 1:
Input:
S = “timetopractice”
P = “toc”
Output : toprac
Explanation: The smallest substring in S that contains “toc” is “toprac”.
Example 2:
Input:
S = “zoomlazapzo”
P = “oza”
Output:
apzo
Explanation:
The smallest substring in S that contains “oza” is “apzo”.
#include<bits/stdc++.h>
using namespace std;
class WindowFinder {
public:
string findSmallestWindow(string s, string p) {
if (p.length() > s.length()) {
return "-1";
} else {
int sHash[26] = {0};
int pHash[26] = {0};
for (int i = 0; i < p.length(); i++) {
pHash[p[i] - 'a']++;
}
int counter = 0;
int begin = 0;
int startIndex = -1;
int length = 0;
int minLength = INT_MAX;
for (int i = 0; i < s.length(); i++) {
sHash[s[i] - 'a']++;
if (pHash[s[i] - 'a'] != 0 && sHash[s[i] - 'a'] <= pHash[s[i] - 'a']) { counter++; } if (counter == p.length()) { while (sHash[s[begin] - 'a'] > pHash[s[begin] - 'a'] || pHash[s[begin] - 'a'] == 0) {
if (sHash[s[begin] - 'a'] > pHash[s[begin] - 'a']) {
sHash[s[begin] - 'a']--;
}
begin++;
}
length = i - begin + 1;
if (length < minLength) {
startIndex = begin;
minLength = length;
}
}
}
if (startIndex == -1) {
return "-1";
} else {
return s.substr(startIndex, minLength);
}
}
}
};
// Test the code
int main() {
string s = "timetopractice";
string p = "toc";
WindowFinder windowFinder;
string smallestWindow = windowFinder.findSmallestWindow(s, p);
// Print the result
cout << smallestWindow << endl;
return 0;
}
class WindowFinder:
def find_smallest_window(self, s, p):
if len(p) > len(s):
return -1
shash = [0] * 26
phash = [0] * 26
for char in p:
phash[ord(char) - ord('a')] += 1
counter = 0
begin = 0
start_index = -1
length = 0
min_length = float('inf')
for i in range(len(s)):
shash[ord(s[i]) - ord('a')] += 1
if phash[ord(s[i]) - ord('a')] != 0 and shash[ord(s[i]) - ord('a')] <= phash[ord(s[i]) - ord('a')]: counter += 1 if counter == len(p): while shash[ord(s[begin]) - ord('a')] > phash[ord(s[begin]) - ord('a')] or phash[ord(s[begin]) - ord('a')] == 0:
if shash[ord(s[begin]) - ord('a')] > phash[ord(s[begin]) - ord('a')]:
shash[ord(s[begin]) - ord('a')] -= 1
begin += 1
length = i - begin + 1
if length < min_length:
start_index = begin
min_length = length
if start_index == -1:
return "-1"
else:
return s[start_index:start_index + min_length]
s = "timetopractice"
p = "toc"
window_finder = WindowFinder()
smallest_window = window_finder.find_smallest_window(s, p)
print(smallest_window)
public class Main{
public static String findSmallestWindow(String s, String p) {
int len1 = s.length();
int len2 = p.length();
if (len1 < len2) {
return "-1";
}
int[] patHash = new int[256];
int[] strHash = new int[256];
for (int i = 0; i < len2; i++) {
patHash[p.charAt(i)]++;
}
int start = 0;
int startIndex = -1;
int minLength = Integer.MAX_VALUE;
int count = 0;
for (int j = 0; j < len1; j++) {
strHash[s.charAt(j)]++;
if (patHash[s.charAt(j)] != 0 && strHash[s.charAt(j)] <= patHash[s.charAt(j)]) { count++; } if (count == len2) { while (strHash[s.charAt(start)] > patHash[s.charAt(start)] || patHash[s.charAt(start)] == 0) {
if (strHash[s.charAt(start)] > patHash[s.charAt(start)]) {
strHash[s.charAt(start)]--;
}
start++;
}
int windowLen = j - start + 1;
if (minLength > windowLen) {
minLength = windowLen;
startIndex = start;
}
}
}
if (startIndex == -1) {
return "-1";
} else {
return s.substring(startIndex, startIndex + minLength);
}
}
// Test the code
public static void main(String[] args) {
String s = "timetopractice";
String p = "toc";
String smallestWindow = findSmallestWindow(s, p);
// Print the result
System.out.println(smallestWindow);
}
}
Question 5 : Spiral Matrix
Problem Statement :
You will be given a 2d matrix. Write the code to traverse the matrix in a spiral format. Check the input and output for better understanding.
Sample Input :
Input :
5 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
Output :
[1, 2, 3, 4, 8, 12, 16, 20, 19, 18, 17, 13, 9, 5, 6, 7, 11, 15, 14, 10]
#include <bits/stdc++.h>
using namespace std;
void Spiral(vector < vector < int >> a) {
if (a.size() == 0) return;
int m = a.size(), n = a[0].size();
int i, k = 0, l = 0;
while (k < m && l < n) {
for (i = l; i < n; ++i) cout << a[k][i] << " ";
k++;
for (i = k; i < m; ++i) cout << a[i][n - 1] << " ";
n--;
if (k < m) { for (i = n - 1; i >= l; --i) cout << a[m - 1][i] << " ";
m--;
}
if (l < n) { for (i = m - 1; i >= k; --i) cout << a[i][l] << " "; l++; } } } int main() { int r, c; cin >> r >> c;
vector < vector < int >> mat(r, vector < int > (c));
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++) cin >> mat[i][j];
Spiral(mat);
}
import java.util.*;
public class Main
{
public static List < Integer > solve(int [][]matrix,int row,int col)
{
List < Integer > res=new ArrayList < Integer > ();
boolean[][] temp=new boolean[row][col];
int []arr1={0,1,0,-1};
int []arr2={1,0,-1,0};
int di=0,r=0,c=0;
for(int i=0;i < row*col; i++) { res.add(matrix[r][c]); temp[r][c]=true; int count1=r+arr1[di]; int count2=c+arr2[di]; if(count1 >= 0 && row > count1 && count2 >= 0 && col > count2 && !temp[count1][count2]){
r=count1;
c=count2;
}
else
{
di=(di+1)%4;
r+=arr1[di];
c+=arr2[di];
}
}
return res;
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int m=sc.nextInt();
int n=sc.nextInt();
int matrix[][]=new int[m][n];
for(int i=0;i < m;i++)
{
for(int j=0;j < n;j++)
matrix[i][j]=sc.nextInt();
}
System.out.println(solve(matrix,m,n));
}
}
def spiralOrder(arr):
ans = []
while arr:
ans += arr.pop(0)
arr = (list(zip(*arr)))[::-1]
return ans
arr = []
n, m = map(int, input().split())
for i in range(n):
arr.append(list(map(int, input().split())))
print(*spiralOrder(arr))
FAQs on Incedo Coding Questions and Answers
Question 1: Does Incedo asks Coding Questions in their Technical Interview ?
Yes, Incedo asks Data Structures and Algorithms based Coding Questions along with other Core CS Subjects based questions to test the Techical Knowledge of the Candidate.
Question 2: How many rounds of interview are there in Incedo?
In, Incedo Recruitment Process for Hiring Freshers includes 1 Technical Interview followed by HR Interview.
Question 3: What are steps involved in Incedo Recruitment Process?
Incedo Recruitment Process involves:
- Online Aptitude + Technical Assessment
- Technical Interview
- HR Interview
Question 4: What is the salary of freshers in Incedo?
Salary offered by Incedo for the Job Profiles like Data Science Trainee or Software Development Trainee is Rs 4.5 LPA – Rs 12 LPA.
Question 5: What is the eligibility criteria for Incedo?
The eligibility criteria for Incedo Recruitment Process is:
- Course : BE/B.Tech/ME/M.Tech/MCA
- Batch : 2022/2023 Batch
- Score : Minimum 65% or equivalent CGPA required throughout the academics.
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
