IBM Previous year Coding Questions
IBM (International Business Machines Corporation) is a global technology leader, renowned for its innovation in AI, cloud computing, quantum computing, and enterprise solutions. Founded in 1911, IBM operates in over 170 countries, delivering cutting-edge services like IBM Watson and IBM Cloud. For students and job seekers, landing a role at IBM—whether as a software engineer, data scientist, or consultant—means joining a prestigious organization that values technical excellence and problem-solving. IBM’s rigorous placement process, including coding assessments, tests skills in algorithms, data structures, and Java, making targeted preparation essential for success.
Dreaming of a career at IBM, the global tech giant revolutionizing AI, cloud, and quantum computing? With over a century of innovation, IBM is a top employer for software engineers, and their campus placements are highly competitive. These previous-year coding questions is your secret weapon to crack their coding rounds!
Freshers (16 Coding Questions)
These focus on fundamental programming problems for entry-level candidates.
Palindrome Check
Problem Statement: Write a program to determine if a given string is a palindrome (reads the same forward and backward, ignoring case and non-alphanumeric characters). Return true if it is a palindrome, false otherwise.
Example: Input: "A man, a plan, a canal: Panama" → Output: true
Constraints: 1 <= s.length <= 2 * 10^5.
Largest Element in Array
Problem Statement: Write a program to find the largest element in an array of integers. Return the largest value.
Example: Input: [3, 5, 1, 9, 2] → Output: 9
Constraints: 1 <= arr.length <= 10^4.
Binary Search
Problem Statement: Implement a binary search algorithm to find the index of a target element in a sorted array. Return the index if found, or -1 if not found.
Example: Input: [1, 3, 5, 7], target = 5 → Output: 2
Constraints: 1 <= arr.length <= 10^4.
Reverse String
Problem Statement: Write a program to reverse the characters of a given string and return the reversed string.
Example: Input: "hello" → Output: "olleh"
Constraints: 1 <= s.length <= 10^5.
Prime Number
Problem Statement: Write a program to determine if a given integer is a prime number (divisible only by 1 and itself). Return true if prime, false otherwise.
Example: Input: 17 → Output: true
Constraints: 1 <= n <= 10^9.
Count Vowels
Problem Statement: Write a program to count the number of vowels (a, e, i, o, u, case-insensitive) in a given string. Return the count.
Example: Input: "Hello World" → Output: 3
Constraints: 1 <= s.length <= 10^5.
Factorial
Problem Statement: Write a program to calculate the factorial of a non-negative integer (n! = n × (n-1) × ... × 1). Return the result.
Example: Input: 5 → Output: 120
Constraints: 0 <= n <= 20.
Exception Handling
Problem Statement: Write a program that takes an array and an index as input and returns the element at that index. Handle ArrayIndexOutOfBoundsException and return -1 if the index is invalid.
Example: Input: [1, 2, 3], index = 4 → Output: -1
Constraints: 1 <= arr.length <= 10^4.
Static Method Implementation
Problem Statement: Write a program with a class containing a static method to calculate the square of a given integer. Call the method without creating an instance and return the result.
Example: Input: 4 → Output: 16
Constraints: -10^4 <= n <= 10^4.
String Comparison
Problem Statement: Write a program to compare two strings using both '==' and 'equals()' methods in Java. Return true if they are equal using 'equals()', false otherwise.
Example: Input: s1 = "hello", s2 = new String("hello") → Output: true
Constraints: 1 <= s1.length, s2.length <= 10^5.
Constructor Implementation
Problem Statement: Write a program to create a class with a parameterized constructor that initializes two integer fields. Create an instance and return the sum of the fields.
Example: Input: a = 3, b = 4 → Output: 7
Constraints: -10^4 <= a, b <= 10^4.
Method Overloading
Problem Statement: Write a program with a class containing two overloaded methods: one to calculate the area of a circle (given radius) and one for a rectangle (given length and width). Return the area based on input.
Example: Input: radius = 2 → Output: 12.566 (approx); Input: length = 3, width = 4 → Output: 12
Constraints: 1 <= radius, length, width <= 10^4.
Number Palindrome*
Problem Statement: Write a program to check if a given integer is a palindrome (same when digits are reversed). Return true if it is a palindrome, false otherwise.
Example: Input: 121 → Output: true
Constraints: -2^31 <= n <= 2^31 - 1.
Fibonacci Series*
Problem Statement: Write a program to generate the nth number in the Fibonacci series (0, 1, 1, 2, 3, 5, …). Return the result.
Example: Input: n = 6 → Output: 8
Constraints: 0 <= n <= 30.
Sum of Digits*
Problem Statement: Write a program to calculate the sum of digits in a given integer. Return the sum.
Example: Input: 123 → Output: 6
Constraints: 0 <= n <= 10^9.
GCD of Two Numbers*
Problem Statement: Write a program to find the Greatest Common Divisor (GCD) of two integers using the Euclidean algorithm. Return the GCD.
Example: Input: 48, 18 → Output: 6
Constraints: 1 <= a, b <= 10^9.
Mid-Level Professionals (16 Coding Questions)
These test intermediate coding skills for candidates with some experience.
Deep Copy Implementation
Problem Statement: Write a program to implement a deep copy of a class with a nested object (e.g., an array). Modify the copied object and show that the original remains unchanged. Return the modified array's sum.
Example: Input: [1, 2, 3] (modify to [4, 5, 6]) → Output: 15
Constraints: 1 <= arr.length <= 10^4.
Hash Table Implementation
Problem Statement: Implement a hash table with put, get, and remove operations using an array and linked lists for collision handling. Return the value for a given key after operations.
Example: Input: put(1, 10), get(1) → Output: 10
Constraints: 1 <= key, value <= 10^6.
Second-Largest Number
Problem Statement: Write a program to find the second-largest number in an array of integers. Return its value, or -1 if it doesn't exist.
Example: Input: [4, 2, 7, 5] → Output: 5
Constraints: 2 <= arr.length <= 10^4.
Singleton Pattern
Problem Statement: Write a program to implement the Singleton design pattern in Java. Ensure only one instance is created and return a counter incremented each time the instance is accessed.
Example: Input: access 3 times → Output: 3
Constraints: Thread safety not required.
Queue Using Stacks
Problem Statement: Write a program to implement a queue using two stacks with enqueue and dequeue operations. Return the dequeued element after a series of operations.
Example: Input: enqueue(1), enqueue(2), dequeue() → Output: 1
Constraints: 1 <= operations <= 10^4.
Remove Duplicates from Sorted Array
Problem Statement: Write a program to remove duplicates from a sorted array in-place and return the length of the new array.
Example: Input: [1, 1, 2, 2, 3] → Output: 3 (array becomes [1, 2, 3])
Constraints: 1 <= arr.length <= 3 * 10^4.
Longest Substring Without Repeating Characters
Problem Statement: Write a program to find the length of the longest substring without repeating characters in a given string.
Example: Input: "abcabcbb" → Output: 3 ("abc")
Constraints: 0 <= s.length <= 5 * 10^4.
Thread Execution
Problem Statement: Write a Java program to create two threads that print numbers from 1 to 5 alternately. Return the concatenated output as a string.
Example: Output: "12345" (interleaved from both threads)
Constraints: Fixed range 1 to 5.
Deadlock Simulation
Problem Statement: Write a Java program to simulate a deadlock with two threads and two resources. Return true if a deadlock is detected, false otherwise.
Example: Input: Thread1 locks A, waits B; Thread2 locks B, waits A → Output: true
Constraints: Two threads, two resources.
Synchronized Method
Problem Statement: Write a Java program with a synchronized method to increment a shared counter accessed by two threads. Return the final counter value after both threads complete.
Example: Input: Each thread increments 5 times → Output: 10
Constraints: 1 <= increments per thread <= 100.
ArrayList Implementation
Problem Statement: Write a program to simulate an ArrayList with add, remove, and get operations. Return the element at a given index after operations.
Example: Input: add(1), add(2), get(1) → Output: 2
Constraints: 1 <= operations <= 10^4.
Functional Interface
Problem Statement: Write a Java program using a functional interface to define a lambda expression that calculates the square of a number. Return the result for a given input.
Example: Input: 5 → Output: 25
Constraints: -10^4 <= n <= 10^4.
Linked List Operations
Problem Statement: Write a program to implement a singly linked list with insert and delete operations. Return the head node's value after operations.
Example: Input: insert(1), insert(2), delete(1) → Output: 2
Constraints: 1 <= operations <= 10^4.
Super vs This
Problem Statement: Write a Java program with a base and derived class, using 'super' to call a base class method and 'this' to access the current object's field. Return the result of the method call.
Example: Input: field = 5, base method adds 10 → Output: 15
Constraints: Simple integer operations.
Anagram Check*
Problem Statement: Write a program to check if two strings are anagrams (contain the same characters with the same frequency, ignoring case). Return true if they are anagrams, false otherwise.
Example: Input: "listen", "silent" → Output: true
Constraints: 1 <= s1.length, s2.length <= 10^5.
Reverse Linked List*
Problem Statement: Write a program to reverse a singly linked list and return the new head node.
Example: Input: 1->2->3 → Output: 3->2->1
Constraints: 1 <= list length <= 10^4.
Experienced Professionals (15 Coding Questions)
These cover advanced coding problems for senior candidates.
Palindrome String
Problem Statement: Write a program to find the longest palindromic substring in a given string. Return the substring.
Example: Input: "babad" → Output: "bab" (or "aba")
Constraints: 1 <= s.length <= 1000.
Custom Thread Pool
Problem Statement: Write a Java program to implement a basic thread pool that executes a fixed number of tasks. Return the sum of results from all tasks (e.g., each task computes a number).
Example: Input: 3 tasks computing 1, 2, 3 → Output: 6
Constraints: 1 <= tasks <= 100.
Find Missing Number
Problem Statement: Write a program to find the missing number in an array of integers from 1 to n. Return the missing number.
Example: Input: [1, 2, 4, 5] → Output: 3
Constraints: 1 <= arr.length <= 10^4.
Merge Sorted Arrays
Problem Statement: Write a program to merge two sorted arrays into a single sorted array. Return the merged array.
Example: Input: [1, 3, 5], [2, 4, 6] → Output: [1, 2, 3, 4, 5, 6]
Constraints: 1 <= arr1.length, arr2.length <= 10^4.
Rotate Matrix
Problem Statement: Write a program to rotate a given n x n matrix 90 degrees clockwise in-place. Return the rotated matrix.
Example: Input: [[1, 2], [3, 4]] → Output: [[3, 1], [4, 2]]
Constraints: 1 <= n <= 20.
Thread-Safe LRU Cache
Problem Statement: Write a program to implement a thread-safe LRU (Least Recently Used) cache with get and put operations. Return the value for a given key after operations.
Example: Input: put(1, 10), get(1) → Output: 10
Constraints: 1 <= capacity <= 3000.
Bill Pugh Singleton
Problem Statement: Write a Java program to implement the Singleton pattern using the Bill Pugh method (static inner class). Return a counter incremented each time the instance is accessed.
Example: Input: access 3 times → Output: 3
Constraints: Thread-safe implementation.
Thread-Safe Singleton with Double-Checked Locking
Problem Statement: Write a Java program to implement a thread-safe Singleton using double-checked locking. Return a counter incremented each time the instance is accessed.
Example: Input: access 3 times → Output: 3
Constraints: Thread-safe implementation.
Observer Pattern
Problem Statement: Write a program to implement the Observer design pattern, where multiple observers are notified of a subject's state change. Return the number of notifications sent.
Example: Input: 3 observers, 2 state changes → Output: 6
Constraints: 1 <= observers <= 10.
HashMap Implementation
Problem Statement: Write a program to implement a basic HashMap with put, get, and remove operations. Return the value for a given key after operations.
Example: Input: put(1, 10), get(1) → Output: 10
Constraints: 1 <= key, value <= 10^6.
Database Transaction Simulation
Problem Statement: Write a program to simulate a database transaction that updates a balance based on a series of deposits and withdrawals. Return the final balance or -1 if any transaction fails (e.g., insufficient funds).
Example: Input: initial = 100, deposit 50, withdraw 30 → Output: 120
Constraints: 1 <= transactions <= 1000.
Callable vs Runnable
Problem Statement: Write a Java program using Callable to compute the sum of an array in a separate thread and return the result via a Future. Compare with Runnable (no return).
Example: Input: [1, 2, 3] → Output: 6
Constraints: 1 <= arr.length <= 10^4.
Saga Pattern Simulation
Problem Statement: Write a program to simulate the Saga pattern for distributed transactions, executing a series of local transactions with compensating actions if one fails. Return true if all succeed, false otherwise.
Example: Input: 3 transactions, 2 succeed, 1 fails → Output: false
Constraints: 1 <= transactions <= 100.
Immutable Object
Problem Statement: Write a Java program to create an immutable class with a list field. Ensure the list cannot be modified after creation and return the sum of its elements.
Example: Input: [1, 2, 3] → Output: 6
Constraints: 1 <= list.length <= 10^4.
Matrix Spiral Traversal*
Problem Statement: Write a program to print the elements of an n x m matrix in spiral order (clockwise from top-left). Return the result as a list.
Example: Input: [[1, 2], [3, 4]] → Output: [1, 2, 4, 3]
Constraints: 1 <= n, m <= 100.
Get the doc file from here- IBM Previous Year coding questions