Add office photos
Employer?
Claim Account for FREE

Adobe

3.9
based on 1.1k Reviews
Video summary
Filter interviews by

20+ Cognizant Interview Questions and Answers

Updated 5 Feb 2024
Popular Designations

Q1. Maximum Sum Problem Statement

You are given an array ARR of N integers. Your task is to perform operations on this array until it becomes empty, and maximize the sum of selected elements. In each operation, sel...read more

Ans.

Given an array, select elements to maximize sum by removing adjacent elements.

  • Iterate through the array and keep track of the count of each element.

  • Select the element with the highest count first, then remove adjacent elements.

  • Repeat the process until the array is empty and sum the selected elements.

Add your answer

Q2. Detect and Remove Loop in Linked List

For a given singly linked list, identify if a loop exists and remove it, adjusting the linked list in place. Return the modified linked list.

Expected Complexity:

Aim for a...read more

Ans.

Detect and remove loop in a singly linked list in place with O(n) time complexity and O(1) space complexity.

  • Use Floyd's Cycle Detection Algorithm to identify the loop in the linked list.

  • Once the loop is detected, use two pointers to find the start of the loop.

  • Adjust the pointers to remove the loop and return the modified linked list.

Add your answer

Q3. Predecessor and Successor in Binary Search Tree (BST)

Given a binary search tree (BST) with 'N' nodes, find the predecessor and successor of a given 'KEY' node in the BST.

Explanation:

The predecessor of a node...read more

Ans.

Find predecessor and successor of a given node in a binary search tree (BST).

  • Predecessor is the node visited just before the given node in an inorder traversal.

  • Successor is the node visited immediately after the given node in an inorder traversal.

  • Return -1 if predecessor or successor does not exist.

  • Implement inorder traversal to find predecessor and successor.

Add your answer

Q4. Count Ways to Reach the N-th Stair Problem Statement

You are provided with a number of stairs, and initially, you are located at the 0th stair. You need to reach the Nth stair, and you can climb one or two step...read more

Ans.

The problem involves counting the number of distinct ways to climb N stairs by taking 1 or 2 steps at a time.

  • Use dynamic programming to solve the problem efficiently.

  • The number of ways to reach the Nth stair is the sum of the number of ways to reach the (N-1)th stair and the (N-2)th stair.

  • Handle base cases for N=0 and N=1 separately.

  • Apply modulo operation to avoid overflow while calculating the result.

  • Consider using memoization to optimize the solution by storing intermediate...read more

Add your answer
Discover Cognizant interview dos and don'ts from real experiences

Q5. Good Arrays Problem Statement

You are given an array 'A' of length 'N'. You must choose an element from any index in this array and delete it. After deleting the element, you will obtain a new array of length '...read more

Ans.

Given an array, find the number of 'good' arrays that can be formed by deleting one element.

  • Iterate through each element in the array and check if deleting it results in a 'good' array

  • Keep track of the sum of elements at odd and even indices to determine if the array is 'good'

  • Return the count of 'good' arrays

Add your answer

Q6. Suppose there is an unsorted array. What will be the maximum window size, such that when u sort that window size, the whole array becomes sorted. Eg, 1 2 6 5 4 3 7 . Ans: 4 (6 5 4 3)

Ans.

Find the maximum window size to sort an unsorted array.

  • Identify the longest decreasing subarray from the beginning and longest increasing subarray from the end

  • Find the minimum and maximum element in the identified subarrays

  • Expand the identified subarrays until all elements in the array are covered

  • The length of the expanded subarray is the maximum window size

View 1 answer
Are these interview questions helpful?

Q7. Longest Common Prime Subsequence Problem Statement

Imagine Ninja is tackling a puzzle during his long summer vacation. He has two arrays of integers, each with lengths 'N' and 'M'. Ninja's task is to determine ...read more

Ans.

Find the length of the longest common prime subsequence between two arrays of integers.

  • Iterate through both arrays to find prime numbers

  • Use a set to keep track of common prime numbers

  • Return the size of the set as the length of the longest common prime subsequence

Add your answer

Q8. Stack using Two Queues Problem Statement

Develop a Stack Data Structure to store integer values using two Queues internally.

Your stack implementation should provide these public functions:

Explanation:

1. Cons...read more
Ans.

Implement a stack using two queues to store integer values with specified functions.

  • Create a stack class with two queue data members.

  • Implement push(data), pop(), top(), size(), and isEmpty() functions.

  • Use one queue for pushing elements and another for temporary storage during operations.

  • Ensure proper handling of edge cases such as empty stack.

  • Example: If input is Q = 5, 1 42, 2, 3, 1 17, the output should be 42, -1, 17.

Add your answer
Share interview questions and help millions of jobseekers 🌟

Q9. What is merge sort and Quick sort. Adv and Disadv of each and which one would u use to sort huge list and Y

Ans.

Merge sort and Quick sort are sorting algorithms used to sort arrays of data.

  • Merge sort is a divide and conquer algorithm that divides the input array into two halves, sorts each half recursively, and then merges the sorted halves.

  • Quick sort is also a divide and conquer algorithm that selects a pivot element and partitions the array around the pivot, sorting the two resulting sub-arrays recursively.

  • Merge sort has a time complexity of O(n log n) and is stable, but requires add...read more

Add your answer

Q10. Puzzle: There is a grid of soldier standing. Soldier ‘A’ is chosen: The tallest men from every column and the shortest among them. Soldier ‘B’ is chosen: The shortest men from every row and the tallest among th...

read more
Ans.

Comparison of heights of two soldiers chosen based on different criteria from a grid of soldiers.

  • Soldier A is chosen based on tallest men from every column and shortest among them.

  • Soldier B is chosen based on shortest men from every row and tallest among them.

  • The height of Soldier A and Soldier B cannot be determined without additional information about the grid of soldiers.

Add your answer

Q11. How to find a loop in a Linked List and how to remove it

Ans.

To find and remove a loop in a Linked List, we can use Floyd's Cycle Detection Algorithm.

  • Use two pointers, slow and fast, to detect if there is a loop in the Linked List

  • If the two pointers meet at some point, there is a loop

  • To remove the loop, set one of the pointers to the head of the Linked List and move both pointers one step at a time until they meet again

  • The meeting point is the start of the loop, set the next pointer of this node to NULL to remove the loop

View 1 answer

Q12. How to find longest last occurring word in a sentence with multiple whitespace

Ans.

Finding the longest last occurring word in a sentence with multiple whitespace.

  • Split the sentence into words using whitespace as delimiter

  • Reverse the list of words

  • Iterate through the list and find the first occurrence of each word

  • Calculate the length of each last occurring word

  • Return the longest last occurring word

Add your answer

Q13. What’s priority queue. How will u make stack and queue with priority queue

Ans.

Priority queue is a data structure that stores elements with priority levels and retrieves them in order of priority.

  • Priority queue is implemented using a heap data structure.

  • Stack can be implemented using a priority queue by assigning higher priority to the most recently added element.

  • Queue can be implemented using a priority queue by assigning higher priority to the oldest element.

Add your answer

Q14. Solve and code the problem of a ball falling from staircase. Each jump can be of 1 step or 2. Find the number of combination of reaching step N

Ans.

Code to find number of combinations of reaching step N by ball falling from staircase with 1 or 2 steps per jump.

  • Use dynamic programming to solve the problem

  • Create an array to store the number of ways to reach each step

  • Initialize the array with base cases for steps 0, 1, and 2

  • Use a loop to fill in the array for steps 3 to N

  • The number of ways to reach step i is the sum of the number of ways to reach step i-1 and i-2

  • Return the value at the Nth index of the array

Add your answer

Q15. What happens when an recursive function is called

Ans.

A recursive function calls itself until a base case is reached, then returns the result to the previous call.

  • Each call creates a new instance of the function on the call stack

  • The function continues to call itself until a base case is reached

  • Once the base case is reached, the function returns the result to the previous call

  • The previous call then continues executing from where it left off

Add your answer
Q16. What is memory protection in operating systems?
Ans.

Memory protection in operating systems prevents one process from accessing or modifying the memory of another process.

  • Memory protection ensures that each process has its own isolated memory space.

  • It prevents unauthorized access to memory locations, improving system stability and security.

  • Operating systems use techniques like virtual memory and access control lists to enforce memory protection.

  • Examples include segmentation faults in Unix-like systems when a process tries to ac...read more

Add your answer
Q17. What are Vtable and VPTR in C++?
Ans.

Vtable and VPTR are used in C++ for implementing polymorphism through virtual functions.

  • Vtable (Virtual Table) is a table of function pointers used to implement dynamic dispatch in C++ for virtual functions.

  • VPTR (Virtual Pointer) is a pointer that points to the Vtable of an object, allowing dynamic binding of virtual functions at runtime.

  • Vtable is created for each class that has virtual functions, containing pointers to the virtual functions of that class.

  • VPTR is a hidden mem...read more

Add your answer
Q18. What are friend functions in C++?
Ans.

Friend functions in C++ are functions that are not members of a class but have access to its private and protected members.

  • Friend functions are declared inside a class with the keyword 'friend'.

  • They can access private and protected members of the class.

  • They are not member functions of the class, but have the same access rights as member functions.

  • Example: friend void displayDetails(Student);

Add your answer

Q19. Define Process &thread

Ans.

Process is an instance of a program while thread is a subset of a process that can run concurrently with other threads.

  • A process is a program in execution

  • A process can have multiple threads

  • Threads share the same memory space as the process

  • Threads can run concurrently with other threads within the same process

  • Examples of processes include web browsers, word processors, and media players

  • Examples of threads include GUI thread, network thread, and background thread

Add your answer

Q20. Implement stack using queue

Ans.

Implementing stack using queue involves using two queues to simulate stack behavior.

  • Create two queues, q1 and q2.

  • Push operation: Enqueue the element to q1.

  • Pop operation: Dequeue all elements from q1 to q2 except the last element. Dequeue and return the last element.

  • Swap the names of q1 and q2 after each pop operation.

  • Top operation: Return the last element of q1 without dequeuing it.

  • isEmpty operation: Check if both q1 and q2 are empty.

Add your answer
Contribute & help others!
Write a review
Share interview
Contribute salary
Add office photos

Interview Process at Cognizant

based on 3 interviews
1 Interview rounds
Coding Test Round
View more
Interview Tips & Stories
Ace your next interview with expert advice and inspiring stories

Top Member Technical Staff Interview Questions from Similar Companies

3.7
 • 26 Interview Questions
4.3
 • 16 Interview Questions
3.5
 • 10 Interview Questions
View all
Share an Interview
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
75 Lakh+

Reviews

5 Lakh+

Interviews

4 Crore+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter