Accenture Preaperation Coding Question Set
Accenture Preaperation Coding Question Set
1>Reverse a no
int n=12345 o/p:- 54321
code:
int n=12345;
int s=0;
int r=0;
while(n>0)
{
r=n%10;
s=(s*10+r);
n=n/10;
}
System.out.println(s);
2>Max no in array
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
int n[]={1,4,6,7,8,9};
int max=0;
Arrays.sort(n);
System.out.println(n[n.length-1]);
}
}
3>find the sub array with the largest sum(kardans algo)
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
int n[]={-2,1,-3,4,-1,2,1,-5,4};
int sum=0;
int max=Integer.MIN_VALUE;
for(int i=0;i<n.length;i++)
{
sum=sum+n[i];
max=Math.max(max,sum);
if(sum<0)
{
sum=0;
}
}
System.out.println(max);
}
}
4>Prime no
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
int n=5;
int count=0;
for(int i=1;i<=n;i++)
{
if(n%i==0)
{
count++;
}
}
if(count==2)
{
System.out.println("prime no");
}
else
{
System.out.println("not prime no");
}
}
}
optimized code:
if(N==1){return 0;}
for(int i=2;i<=Math.sqrt(N);i++)
{
if(N%i==0)
{
return 0;
}
}
return 1;
10>the function accept a string STR at its argument the function need to return the
transform the
string by replacing all occurence of character a to b and vice versa?
code:
String s="abaabbcc";
char c[]=s.toCharArray();
for(int i=0;i<s.length();i++)
{
if(c[i]=='a')
{
c[i]='b';
}
else if(c[i]=='b')
{
c[i]='a';
}
}
String ans=new String(c);
System.out.println(ans);
11>check whether string is pallendrom or not
code :
without using function
String s="abba";
String ans=s;
int a=s.length();
char c[]=s.toCharArray();
char c1[]=new char[a];
for(int i=c.length-1,j=0;i>=0;i--,j++)
{
c1[j]=c[i];
}
String ans1=new String(c1);
if(ans.equals(ans1))
{
System.out.println("pallendrom no");
}
else
{
System.out.println("not pallendrom");
}
}
with using function
// code here
StringBuffer sb=new StringBuffer(S);
sb.reverse();
String ans=sb.toString();
if(ans.equals(S))
{
return 1;
}
return 0;
12>Reverse the words
i/p:hello world
o/p world hello;
code:
String s="Hello, World!";
String []word=s.split(" ");
for(int i=word.length-1;i>=0;i--)
{
System.out.print(word[i]+" ");
}
13>count the occurence in the array of give element
code:
int n[]={5,2,4,1,2};
int count=0;
int el=2;
for(int ans:n)
{
if(ans==el)
{
count++;
}
}
System.out.println(count);
}
or
String ans=" ";
String a[]=S.split("\\.");
for(int i=a.length-1;i>=0;i--)
{
ans=ans+a[i];
if(i>0)
{
ans=ans+".";
}
}
return ans;
14>Calculate and return the difference between the sum of square roots of even
number and sum of square root of odd no in the rance from m to n.
public static void main(String[] args) {
int n=1,m=10;
double esum=0,osum=0;
for(int i=n;i<=m;i++)
{
if(i%2==0)
{
esum=esum+Math.sqrt(i);
}
else if(i%2!=0)
{
osum=osum+Math.sqrt(i);
}
}
System.out.println(esum);
System.out.println(osum);
System.out.println("SBustraction is:"+(esum-osum));
psvm(String args[])
{
int row=matrix.length;
int col=matrix[0].length;
int low=0;int high=row*col-1;
while(low<=high)
{
int mid(low+high)/2;
if(matrix[mid/col][mid%col]==target)
{
return true;
}
else if(matrix[mid/col][mid%col]<target)
{
low=mid+1;
}else
{
high=mid-1;
}
}
return false;
}
25>given a string as consisting of word and space return the length of the last
word in the string gfg prctice?
code:
int findLength(String s)
{
String sp[]=s.split(" ");
return(sp[sp.length-1]).length();
}
split: eg: "hello world"
n[]={"hello","world"};// split kerega ye ki " " yaha pe se array ek ek
position pe data element puch ho reha h.
26.>Set entire Matrix row and column as Zeroes (Set Matrix Zeroes)?
code:
int row=matrix.length;
int col=matrix[0].length;
int rowa[]=new int[row];
int cola[]=new int[col];
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++){
if(matrix[i][j]==0)
{
rowa[i]=1;
cola[i]=1;
}
}
}
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
if(rowa[i]==1 || cola[i]==1)
{
matrix[i][j]=0;
}
}
}
27>Palindrome Linked List
logic ye h ki sbse phele hum middle find kerenge then, uss middle se aage tk
reverse ker denge aur fir middle tk aur uske bd ke data ko
compare kerenge agar same hua to pallendrom ni to ni
code:
class Solution {
public:
// Function to check whether the list is palindrome.
Node* getMid(Node * head){
Node* slow = head;
Node * fast = head ->next;
while(curr != NULL){
next = curr -> next;
curr -> next = prev;
prev = curr;
curr = next;
}
return prev;
}
public:
// Function to check whether the list is palindrome.
bool isPalindrome(Node *head) {
// Your code here
if(head -> next == NULL){
return true;
}
Node* middle = getMid(head);
while(head2 != NULL){
if(head1 -> data != head2 -> data){
return false;
}
head1 = head1 -> next;
head2 = head2 -> next;
}
return true;
}
};
28>Find the difference between two successive in an array
code;
int n[]={1,3,6,9};
int max=0;
int sub=0;
for(int i=0;i<n.length;i++){
for(int j=i+1;j<n.length-1;j++)
{
sub=n[j]-n[i];
if(sub>max)
{
max=sub;
}
}
}
System.out.println(sub);
or
int n[]={1,3,6,9};
int diff=Integer.MIN_VALUE;
for(int i=1;i<n.length;i++)
{
diff=Math.max(diff,n[i]-n[i-1]);
}
System.out.println(diff);
29>Superior ELEMENT
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
int n[]={1,8,5,0,6};
int e=0,count=0;
for(int i=n.length-1;i>=0;i--)
{
if(n[i]>e)
{
e=n[i];
count++;
}
}
System.out.println(count);
}
}
30>The given function has a string (str) and two characters, chi and ch2. Execute
the function in such a way that str returns to its original string, and all
the events in ch¹ are replaced by ch2, and vice versa.
Test Case 1:
code:
public String removeStars(String s) {
Stack<Character>st=new Stack<>();
for(int i=0;i<s.length();i++)
{
char c=s.charAt(i);
if(c=='*')
{
if(!st.isEmpty())
{
st.pop();
}
}
else
{
st.push(c);
}
}
StringBuilder result = new StringBuilder();
while (!st.isEmpty()) {
result.append(st.pop());
}
return result.reverse().toString();
}
33>Bob desires to buy N candies. The price of each candy is given in an array. Bob
has M amount of money. The best deal for him in the supermarket is that if the
price of a candy is a multiple of 5 then he doesn't have to pay, for the rest he
has to pay the amount mentioned in A[i], that is the exact amount
Input format:
input1: Number of candies N
Ouput Format:
N=3
Arr[5,15,105]
M=8
olp: 3
code:
int n[]={5,10,105};
int m=5;
int a=4;
int count=0;
for(int i=0;i<n.length;i++)
{
if(n[i]%5==0)
{
count++;
}
else if(n[i]<m)
{
count++;
m=m-n[i];
}
}
System.out.println(count);
32>Given a string in Input which is nothing words sebanated by full stop () And we
have to but the return langest size of word.
Eg:
hell.my.name.is.shaurya"
o/p: "shaurya
code:
public static void main(String[] args) {
String n="hello.my.name.is.shaurya";
String ans[]=n.split("\\.");
String ans1="";
int max=0;
for(int i=0;i<ans.length;i++)
{
if(ans[i].length()>max)
{
max=ans[i].length();
}
}
for(int i=0;i<ans.length;i++)
{
if(ans[i].length()==max)
{
ans1=ans[i];
}
}
System.out.println(ans1);
}
33>Jack has an array A of length N. He wants to label whether the number in the
array is even or odd. Your task is to help him find and
return a string with labels even or odd in sequence according to which the numbers
appear in the array.