0% found this document useful (0 votes)
39 views

Problem Statement TCS

tcs paper

Uploaded by

Aman Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Problem Statement TCS

tcs paper

Uploaded by

Aman Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

1.

Problem Statement –

A chocolate factory is packing chocolates into the packets. The chocolate packets here represent an
array of N number of integer values. The task is to find the empty packets(0) of chocolate and push it to
the end of the conveyor belt(array).

Example 1 :

N=8 and arr = [4,5,0,1,9,0,5,0].

There are 3 empty packets in the given set. These 3 empty packets represented as O should be pushed
towards the end of the array

Input :

8 – Value of N

[4,5,0,1,9,0,5,0] – Element of arr[O] to arr[N-1],While input each element is separated by newline

Output:

45195000

Example 2:

Input:

6 — Value of N.

[6,0,1,8,0,2] – Element of arr[0] to arr[N-1], While input each element is separated by newline

Output:

618200

Code-
import java.util.*;
class Main
{
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();
int count=0;
for(int i=0;i< n;i++)
if(arr[i]!=0)
arr[count++]=arr[i];
for(int i=count;i < n;i++)
arr[i]=0;
for(int i=0;i< n;i++)
System.out.print(arr[i]+" ");
}
}
2. Problem Statement –

Joseph is learning digital logic subject which will be for his next semester. He usually tries to solve unit
assignment problems before the lecture. Today he got one tricky question. The problem statement is “A
positive integer has been given as an input. Convert decimal value to binary representation. Toggle all bits
of it after the most significant bit including the most significant bit. Print the positive integer value after
toggling all bits”.

Constrains-

1<=N<=100

Example 1:

Input :

10 -> Integer

Output :

5 -> result- Integer

Explanation:

Binary representation of 10 is 1010. After toggling the bits(1010), will get 0101 which represents “5”.
Hence output will print “5”.

Code –

import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int no=sc.nextInt();
String bin="";

while(no!=0)
{
bin=(no&1)+bin;
no=no>>1;
}
bin=bin.replaceAll("1","2");
bin=bin.replaceAll("0","1");
bin=bin.replaceAll("2","0");
int res=Integer.parseInt(bin,2);
System.out.println(res);
}
}
Jack is always excited about sunday. It is favourite day, when he gets to play all day. And goes to cycling
with his friends.

So every time when the months starts he counts the number of sundays he will get to enjoy. Considering
the month can start with any day, be it Sunday, Monday…. Or so on.

Count the number of Sunday jack will get within n number of days.

Example 1:

Input

mon-> input String denoting the start of the month.

13 -> input integer denoting the number of days from the start of the month.

Output :

2 -> number of days within 13 days.

Explanation:

The month start with mon(Monday). So the upcoming sunday will arrive in next 6 days. And then next
Sunday in next 7 days and so on.

Now total number of days are 13. It means 6 days to first sunday and then remaining 7 days will end up in
another sunday. Total 2 sundays may fall within 13 days.

Code –
import java.util.*;
class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str=sc.next();
int n=sc.nextInt();
String arr[]={"mon","tue,","wed","thu","fri","sat","sun"};
int i=0;
for(i=0;i< arr.length;i++) if(arr[i].equals(str)) break; int
res=1; int rem=6-i; n=n-rem; if(n >0)
res+=n/7;
System.out.println(res);

}
}
Given an integer array Arr of size N the task is to find the count of elements whose value is greater than all
of its prior elements.

Note : 1st element of the array should be considered in the count of the result.

For example,

Arr[]={7,4,8,2,9}

As 7 is the first element, it will consider in the result.

8 and 9 are also the elements that are greater than all of its previous elements.

Since total of 3 elements is present in the array that meets the condition.

Hence the output = 3.

Example 1:

Input

5 -> Value of N, represents size of Arr

7-> Value of Arr[0]

4 -> Value of Arr[1]

8-> Value of Arr[2]

2-> Value of Arr[3]

9-> Value of Arr[4]

Output :

Example 2:

5 -> Value of N, represents size of Arr

3 -> Value of Arr[0]

4 -> Value of Arr[1]


5 -> Value of Arr[2]

8 -> Value of Arr[3]

9 -> Value of Arr[4]

Output :

Constraints

1<=N<=20

1<=Arr[i]<=10000

Code-
import java.util.*;
class Main
{
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();
int max=Integer.MIN_VALUE;

int count=0;
for(int i=0;i< n;i++) { if(arr[i]>max)

{
max=arr[i];
count++;
}
}
System.out.println(count);
}
}
Problem Statement

An intelligence agency has received reports about some threats. The reports consist of numbers in a
mysterious method. There is a number “N” and another number “R”. Those numbers are studied
thoroughly and it is concluded that all digits of the number ‘N’ are summed up and this action is performed
‘R’ number of times. The resultant is also a single digit that is yet to be deciphered. The task here is to find
the single-digit sum of the given number ‘N’ by repeating the action ‘R’ number of times.

If the value of ‘R’ is 0, print the output as ‘0’.

Example 1:

Input :

99 -> Value of N

3 -> Value of R

Output :

9 -> Possible ways to fill the cistern.

Explanation:

Here, the number N=99

1. Sum of the digits N: 9+9 = 18


2. Repeat step 2 ‘R’ times i.e. 3 tims (9+9)+(9+9)+(9+9) = 18+18+18 =54
3. Add digits of 54 as we need a single digit 5+4
Hence , the output is 9.

Example 2:

Input :

1234 -> Value of N

2 -> Value of R

Output :

2 -> Possible ways to fill the cistern

Explanation:
Here, the number N=1234

1. Sum of the digits of N: 1+2+3+4 =10


2. Repeat step 2 ‘R’ times i.e. 2 times (1+2+3+4)+(1+2+3+4)= 10+10=20
3. Add digits of 20 as we need a single digit. 2+0=2
Hence, the output is 2.

Constraints:

0<N<=1000

0<=R<=50

The Input format for testing

The candidate has to write the code to accept 2 input(s)

First input- Accept value for N (positive integer number)

Second input: Accept value for R(Positive integer number)

The output format for testing

The output should be a positive integer number or print the message (if any) given in the problem
statement. (Check the output in Example 1, Example 2).

Code –
import java.util.*;
class Main
{
public static int sumOfDigits(int n)
{
int sum=0;
while(n>0)
{
sum+=n%10;
n=n/10;
}
return sum;
}
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int r=sc.nextInt();
if(r==0)
System.out.println("0");
else
{
int res=sumOfDigits(n)*r;
int sum=0;
while(true)
{
while(res>0)
{
sum=sum+res%10;
res=res/10;
}
if((sum/10)==0)
break;
else
res=sum;
}
System.out.println(sum);
}
}
1. Reverse a string in Python.

def reverse_string(s):

return s[::-1]

# Example usage:

print(reverse_string("hello")) # Output: "olleh"

2. Check if a string is a palindrome in Python.

def is_palindrome(s):

return s == s[::-1]

# Example usage:

print(is_palindrome("racecar")) # Output: True

3. Find the factorial of a number in Python.

def factorial(n):

if n == 0:

return 1

else:

return n * factorial(n-1)
# Example usage:

print(factorial(5)) # Output: 120

4. Implement a binary search algorithm in Python.

def binary_search(arr, target):

low, high = 0, len(arr) - 1

while low <= high:

mid = (low + high) // 2

if arr[mid] == target:

return mid

elif arr[mid] < target:

low = mid + 1

else:

high = mid - 1

return -1

# Example usage:

print(binary_search([1, 2, 3, 4, 5], 3)) # Output: 2

5. Implement a bubble sort algorithm in Python.

def bubble_sort(arr):

n = len(arr)
for i in range(n):

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

if arr[j] > arr[j+1]:

arr[j], arr[j+1] = arr[j+1], arr[j]

return arr

# Example usage:

print(bubble_sort([5, 3, 2, 4, 1])) # Output: [1, 2, 3, 4, 5]

6. Find the maximum element in an array in Python.

def find_max(arr):

max_element = arr[0]

for num in arr:

if num > max_element:

max_element = num

return max_element

# Example usage:

print(find_max([5, 3, 9, 1, 7])) # Output: 9

7. Calculate the Fibonacci sequence up to a certain number in Python.

def fibonacci(n):
fib_sequence = [0, 1]

while fib_sequence[-1] < n:

fib_sequence.append(fib_sequence[-1] + fib_sequence[-2])

return fib_sequence[:-1]

# Example usage:

print(fibonacci(50)) # Output: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

8. Remove duplicates from a list in Python.

def remove_duplicates(arr):

return list(set(arr))

# Example usage:

print(remove_duplicates([1, 2, 2, 3, 4, 4, 5])) # Output: [1, 2, 3, 4, 5]

9. Check if a number is prime in Python.

def is_prime(n):

if n <= 1:

return False

for i in range(2, int(n**0.5) + 1):

if n % i == 0:

return False

return True
# Example usage:

print(is_prime(17)) # Output: True

10. Find the sum of digits of a number in Python.

def sum_of_digits(n):

return sum(int(digit) for digit in str(n))

# Example usage:

print(sum_of_digits(12345)) # Output: 15

11. Implement a stack in Python.

class Stack:

def __init__(self):

self.items = []

def push(self, item):

self.items.append(item)

def pop(self):

if not self.is_empty():

return self.items.pop()
def is_empty(self):

return len(self.items) == 0

def peek(self):

if not self.is_empty():

return self.items[-1]

def size(self):

return len(self.items)

# Example usage:

stack = Stack()

stack.push(1)

stack.push(2)

print(stack.peek()) # Output: 2

stack.pop()

print(stack.peek()) # Output: 1

12. Implement a queue in Python.

class Queue:

def __init__(self):

self.items = []
def enqueue(self, item):

self.items.append(item)

def dequeue(self):

if not self.is_empty():

return self.items.pop(0)

def is_empty(self):

return len(self.items) == 0

def size(self):

return len(self.items)

# Example usage:

queue = Queue()

queue.enqueue(1)

queue.enqueue(2)

print(queue.dequeue()) # Output: 1

13. Find the intersection of two arrays in Python.

def intersection(arr1, arr2):


return list(set(arr1) & set(arr2))

# Example usage:

print(intersection([1, 2, 3, 4], [3, 4, 5, 6])) # Output: [3, 4]

14. Count the occurrences of each word in a sentence in Python.

def word_count(sentence):

words = sentence.split()

word_count_dict = {}

for word in words:

if word in word_count_dict:

word_count_dict[word] += 1

else:

word_count_dict[word] = 1

return word_count_dict

# Example usage:

print(word_count("the quick brown fox jumps over the lazy dog"))

# Output: {'the': 2, 'quick': 1, 'brown': 1, 'fox': 1, 'jumps': 1, 'over': 1, 'lazy': 1, 'dog': 1}

15. Implement a linked list in Python.

class Node:

def
16. Find the missing number in an array containing 1 to n in Python.

def find_missing_number(nums):

n = len(nums) + 1

total_sum = n * (n + 1) // 2

actual_sum = sum(nums)

return total_sum - actual_sum

# Example usage:

print(find_missing_number([1, 2, 4, 5])) # Output: 3

17. Check if two strings are anagrams of each other in Python.

def are_anagrams(s1, s2):

return sorted(s1) == sorted(s2)

# Example usage:

print(are_anagrams("listen", "silent")) # Output: True

18. Find the first non-repeating character in a string in Python.

def first_non_repeating_char(s):

char_count = {}

for char in s:

if char in char_count:

char_count[char] += 1
else:

char_count[char] = 1

for char in s:

if char_count[char] == 1:

return char

return None

# Example usage:

print(first_non_repeating_char("hello")) # Output: 'h'

19. Rotate an array to the right by k steps in Python.

def rotate_array(nums, k):

k %= len(nums)

nums[:] = nums[-k:] + nums[:-k]

# Example usage:

nums = [1, 2, 3, 4, 5]

rotate_array(nums, 2)

print(nums) # Output: [4, 5, 1, 2, 3]

20. Find the intersection of two linked lists in Python.

class ListNode:

def __init__(self, val=0, next=None):

self.val = val
self.next = next

def get_intersection_node(headA, headB):

pointerA, pointerB = headA, headB

while pointerA != pointerB:

pointerA = pointerA.next if pointerA else headB

pointerB = pointerB.next if pointerB else headA

return pointerA

# Example usage:

# Construct linked lists

intersect_node = ListNode(3)

intersect_node.next = ListNode(4)

headA = ListNode(1)

headA.next = ListNode(2)

headA.next.next = intersect_node

headB = ListNode(5)

headB.next = ListNode(6)

headB.next.next = intersect_node
intersection = get_intersection_node(headA, headB)

print(intersection.val) # Output: 3

You might also like