Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
59 views

Accenture Preaperation Coding Question Set

Uploaded by

nitinsr246
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Accenture Preaperation Coding Question Set

Uploaded by

nitinsr246
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 13

ACCENTURE MOST ASKED CODING QUESTION

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;

5>Find target element in an array


import java.util.*;
class HelloWorld {
public static void main(String[] args) {
int n[]={2,3,4,10,40};
int target =4;
int si=0;int ei=n.length-1;
int mid=si+(ei-si)/2;
while(si<ei)
{
if(n[mid]==target)
{
int ans=mid+1;
System.out.println("data found at:"+ans);
break;
}
else if(target>mid)
{
si=mid+1;
}
else
{
ei=mid-1;
}
mid=si+(ei-si)/2;
}
}
}
6>Find Middle element in an linkedlist
7>Find loop in a linkedlist
8>Check for anagram
code:
public static boolean isAnagram(String a, String b) {

// Your code here


if (a.length() != b.length()) {
return false;}
char ch[]=a.toCharArray();
char ch1[]=b.toCharArray();
Arrays.sort(ch);
Arrays.sort(ch1);
String s=new String(ch);
String s1=new String(ch1);
if (s.equals(s1))
{
return true;
}
else return false;
}
}
9>Find Missing no in array
code:
int missingNumber(int n, int arr[]) {
int tot=(n*(n+1))/2;
int sum=0;
for(int i=0;i<n-1;i++)
{
sum=sum+arr[i];
}
return(tot-sum);

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));

15>Search in a matrix(by using binary search)

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;
}

16.Check if two String array are equivalent or not


import java.util.*;
class HelloWorld {
public static void main(String[] args) {
String n[]={"ab","c"};
String n1[]={"a","bc"};
StringBuilder sb=new StringBuilder();
StringBuilder sb1=new StringBuilder();
for(int i=0;i<n.length;i++)
{
sb.append(n[i]);
}
for(int i=0;i<n1.length;i++)
{
sb.append(n1[i]);
}
String ans1=sb.toString();
String ans2=sb.toString();
if(ans1.equals(ans2))
{
System.out.println("true");
}
else
{
System.out.println("false");
}
}
}

17.Best time to buy and sell stock


code:
int min=Integer.MAX_VALUE;
int max=Integer.MIN_VALUE;
for(int i=0;i<prices.length;i++)
{
min=Math.min(min,price[i]);
max=Math.max(max,price[i]-min);
}
return max;

18. Fabonnic series


import java.util.*;
class HelloWorld {
static int nthFibonacci(int n){
// code here
if(n<=1)
{
return n;
}
return nthFibonacci(n-1)+nthFibonacci(n-2);
}
}
19.No of occurence of each element in array
import java.util.*;
class HelloWorld {
public static void main(String args[])
{
int n[]={1,2,3,4,1,2};
Arrays.sort(n);
HashMap<Integer,Integer>hm=new HashMap<>();
for(int element :n)
{
if(hm.containsKey(element))
{
hm.put(element,hm.get(element)+1);
}
else
{
hm.put(element,1);
}
}
for(int ans:hm.keySet())
{
System.out.println(ans+"-"+hm.get(ans));
}
}
20>Rearrange an array element by sign.
// i/p:3,1,-2,-5,2,-4
//o/p:3,-2,1,-5,2,-4
import java.util.*;
class HelloWorld {
public static void main(String args[])
{
int n[]={3,1,-2,-5,2,-4};
ArrayList<Integer>al=new ArrayList<>();
ArrayList<Integer>al1=new ArrayList<>();
ArrayList<Integer>ans=new ArrayList<>();
for(int i=0;i<n.length;i++)
{
if(n[i]>0)
{
al.add(n[i]);
}
else if(n[i]<0)
{
al1.add(n[i]);
}
}
int size=Math.min(al.size(),al1.size());
for(int i=0;i<size;i++)
{
ans.add(al.get(i));
ans.add(al1.get(i));
}
for(int anss:ans)
{
System.out.println(anss);
}
}
}
21>Write a program to find if given year is leap year
import java.util.*;
class HelloWorld {
public static void main(String args[])
{
int n= 2024;
if(n%400==0 || (n%100!=0 &&n%4==0))
{
System.out.println("leap year");
}
else {
System.out.println("not leap year");
}
}
}
22>Factorial of an no?
23>sum of divisor of no integer
import java.util.*;
class HelloWorld {
public static void main(String args[])
{
int n= 54;
int sum=0;
for(int i=1;i<=n;i++)
{
if(n%i==0)
{
sum=sum+i;
}
}
System.out.println(sum);
}
}
24>Merge Sorted Array.

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(fast != NULL && fast -> next != NULL){


fast = fast -> next -> next;
slow = slow -> next;
}
return slow;
}
Node* reverse(Node* head){
Node* curr = head;
Node* prev = NULL;
Node* next = NULL;

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);

Node* temp = middle -> next;


middle -> next = reverse(temp);

Node* head1 = head;


Node* head2 = middle->next;

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:

• Input: str: "apples", chi: 'a', ch2: 'p'

• Expected Output: "paales"


import java.util.*;
class HelloWorld {
public static void main(String[] args) {
String n="apples";
char ch1='a',ch2='p';
char c[]=n.toCharArray();
for(int i=0;i<c.length;i++)
{
if(c[i]==ch1)
{
c[i]=ch2;
}
else if(c[i]==ch2)
{
c[i]=ch1;
}
}
String ans=new String(c);
System.out.println(ans);
}
}
31>Index of the First Occurrence of pattern in a text?
Input:
text = gffgfg
pattern = gfg
Output: 3
Explanation: Considering 0-based indexing, substring of text from 3rd to last
index is gfg.
code:
public int findMatching(String text, String pat) {
// Code here
return text.indexOf(pat);
}

32> Removing Stars From a String


Input: s = "leet**cod*e"
Output: "lecoe"
Explanation: Performing the removals from left to right:
- The closest character to the 1st star is 't' in "leet**cod*e". s becomes
"lee*cod*e".
- The closest character to the 2nd star is 'e' in "lee*cod*e". s becomes "lecod*e".
- The closest character to the 3rd star is 'd' in "lecod*e". s becomes "lecoe".
There are no more stars, so we return "lecoe".

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

input2: Array of the price of candies

input3: Amount of money M

Ouput Format:

Return the maximum number of candies he can buy


ex 1

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.

N=5 Arr = [1,2,3,4,5]


"Odd Even Odd Even Odd"
code:
public static void main(String[] args) {
int n[]={1,2,3,4,5};
String s=" ";
StringBuilder sb=new StringBuilder();
for(int i=0;i<n.length;i++)
{
if(n[i]%2!=0)
{
s=s+"Odd";
OR
sb.append("Odd");
}
else if(n[i]%2==0)
{
s=s+"Even";
OR
sb.append("Even");
}
}
String ans=new String(sb);
System.out.println(ans);
OR
System.out.println(s);
}

You might also like