Accenture Coding Question
Accenture Coding Question
One Shot
Q.1 Reverse a number .
int number = 987654
class Main {
public static void main(String[] args) {
}
return max;
}
}
Q.4 Write a code for Prime Number .
Prime number is a number that is greater than 1 and divided by 1 or
itself only.
1. public static boolean isPrime(int n) {
2. if (n <= 1) {
3. return false;
4. }
5. for (int i = 2; i <= Math.sqrt(n); i++) {
6. if (n % i == 0) {
7. return false;
8. }
9. }
10. return true;
11. }
12.}
Q.5 Find target element in an array .
• int[] array = {2, 3, 4, 10, 40};
• int target = 10;
public class BinarySearch {
if (array[mid] == target) {
return mid;
}
return -1;
}
Q.6 Find the middle of linkedlist
class Solution {
public ListNode middleNode(ListNode head) {
ListNode slow=head,fast=head;
while(fast!=null && fast.next!=null){
slow=slow.next;
fast=fast.next.next;
}
return slow;
}
}
Q.7 Given head, the head of a linked list, determine
if the linked list has a cycle in it.
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode slow= head, fast=head;
while(fast != null && fast.next !=null ){
slow =slow.next;
fast=fast.next.next;
if(slow==fast)
return true;
}
return false;
}
}
Q.8 Given two strings s and t, return true if t is
an anagram of s, and false otherwise.
An Anagram is a word or phrase formed by rearranging the letters of a
different word or phrase, typically using all the original letters exactly
once.
Input: s = "anagram", t = "nagaram"
Output: true
class Solution {
public boolean isAnagram(String s, String t) {
if(s.length()!=t.length()){
return false;
}
int map[]=new int[26];
for(int i=0;i<s.length();i++){
map[s.charAt(i)-'a']++;
map[t.charAt(i)-'a']--;
}
for(int i=0;i<26;i++){
if(map[i]!=0){
return false;
}
}
return true;
}
}
Q.9 Find the missing number in an array .
Given an array nums containing n distinct numbers in the range [0, n],
return the only number in the range that is missing from the array.
Input: nums = [3,0,1]
Output: 2
class Solution {
public int missingNumber(int[] nums) {
int n=nums.length;
int total=(n)*(n+1)/2;
int sum=0;
for(int i=0;i<nums.length;i++){
sum+=nums[i];
}
return total-sum;
}
}
Q.10 The function accepts a string ‘str’ as its argument. The
function needs to return the transformed string by replacing all
occurrences of the character ‘a’ with the character ‘b’ and vice
versa.
Input :- abaabbcc
public class StringTransformation {
public static String transformString(String str) {
char[] charArray = str.toCharArray();
for (int i = 0; i < charArray.length; i++) {
if (charArray[i] == 'a')
charArray[i] = 'b';
else if (charArray[i] == 'b')
charArray[i] = 'a';
}
return new String(charArray);
}
public static void main(String[] args) {
String str = "abaabbcc";
System.out.println(transformString(str));
}
Q.11 Check whether string is palindrome or
not.
public static boolean isPalindrome(String str) {
int left = 0;
int right = str.length() - 1;
while (left < right) {
if (str.charAt(left) != str.charAt(right)) {
return false; // Not a palindrome
}
left++;
right--;
}
return true;
}
Q.12 The function accepts a string ‘str’ as its
argument. The function needs to reverse the order
of the words in the string.
Input :- String str = "Hello, World!";
public class ReverseWords {
public static void reverseWords(String str) {
String[] words = str.split(" ");
for (int i = words.length - 1; i >= 0; i--) {
System.out.print(words[i] + " ");
}
}
public static void main(String[] args) {
String str = "Hello, World!";
reverseWords(str);
}
}
Q.13 Count the Occurrences of a Given
Element in an Array.
• int[] arr = {5, 2, 4, 1, 2};
• int element = 2;
public class OccurrenceCounter {
public static int countOccurrences(int[] arr, int element) {
int count = 0;
for (int num : arr) {
if (num == element) {
count++;
}
}
return count;
}
public static void main(String[] args) {
int[] arr = {5, 2, 4, 1, 2};
int element = 2;
int result = countOccurrences(arr, element);
System.out.println(result);
}
}
Q.14 Calculate and return the difference between the sum of
square roots of even numbers and the sum of square roots of
odd numbers in the range from ‘m’ to ‘n’ (inclusive).
}
Q.16 Check If Two String Arrays are Equivalent
Input: word1 = ["ab", "c"], word2 = ["a", "bc"]
class Solution {
public boolean arrayStringsAreEqual(String[] word1, String[] word2) {
StringBuilder string1 =new StringBuilder();
StringBuilder string2 =new StringBuilder();
for(int i=0;i<word1.length;i++){
string1.append(word1[i]);
}
for(int i=0;i<word2.length;i++){
string2.append(word2[i]);
}
if(string1.toString().equals(string2.toString()))
return true;
else
return false;
}
}
Q.17 Inorder Traversal
public class BinaryTreeInorderTraversal {
if (root == null) {
return;
}
// Recursively traverse the left subtree
inorder(root.left, arr);
arr.add(root.data);
inorder(root.right, arr);
}
Q.18 Preorder Traversal
public class BinaryTreeTraversal {
if (root == null) {
return;
}
arr.add(root.data);
preorder(root.left, arr);
preorder(root.right, arr);
}
Q.19 Post Order Traversal
public class BinaryTreeTraversal {
static void postorder(Node root, List<Integer> arr){
if(root == null){
return;
}
postorder(root.left, arr);
postorder(root.right, arr);
arr.add(root.data);
}
Q.20 Best Time to Buy and Sell Stock
You want to maximize your profit by choosing a single day to buy one
stock and choosing a different day in the future to sell that stock.
}
}
Q.22 Given an array, we have found the number of
occurrences of each element in the array.
Input: arr[] = {10,5,10,15,10,5};
public class ElementFrequency {
public static void main(String[] args) {
int[] array = {1, 2, 2, 3, 4, 4, 4, 5};
You are given a 0-indexed integer array nums of even length consisting
of an equal number of positive and negative integers.
arr[k]=positive.get(c);
arr[k+1]=negative.get(c);
k+=2;
c++;
}
return arr;
Q.24 Write a Program to Find if a Given Year is
a Leap Year.
#include<bits/stdc++.h>
int main() {
int year;
if (year % 400 == 0)
cout << year << " is a Leap Year";
else
cout << year << " is not a Leap Year";
return 0;
}
Q.25 Write a program for factorial of number.
Input N=5 ;
public static long factorialIterative(int n)
{
long result = 1;
Input : N=48;
public static int sumOfDivisors(int n) {
int sum = 0;
for (int i = 1; i <= n; i++) {
if (n % i == 0) {
sum += i;
}
}
return sum;
}
Q.27 Merge Sorted Array
• Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
• Output: [1,2,2,3,5,6]
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int i=0,j=0,k=0;
int arr[]=new int [m+n];
while(i<m && j<n){
if(nums1[i]<nums2[j]){
arr[k++]=nums1[i++];
}
else{
arr[k++]=nums2[j++];
}
}
while(i<m){
arr[k++]=nums1[i++];
}
while(j<n){
arr[k++]=nums2[j++];
}
for(int c=0;c<arr.length;c++){
nums1[c]=arr[c];
}
}
}
Q.28 Write a program to find the sum of Natural
Numbers using Recursion
Input :N=10
public class Main
{
public static void main (String[]args)
{
int n = 10;
int sum = getSum (n);
System.out.println (sum);
}