Infosys interview prep
11 practice questions on the topics candidates reported from Infosys interviews, with answers. The questions are ours; each one links to the interview report that names its topic.
DSA 7
-
In an unweighted graph, which algorithm finds the path with the fewest edges from a start vertex to every other vertex?
- Depth-first search
- Kruskal's algorithm
- Topological sort
- Breadth-first search
Show the answer
D. Breadth-first search
BFS visits vertices in order of their distance in edges, so the first time it reaches a vertex is along a shortest path. DFS can reach a vertex by a long path first.
This topic is named in a candidate’s interview report: Specialist Programmer interview, 2026, on GeeksforGeeks.
-
For a positive integer n, what does (n & (n - 1)) == 0 tell you?
- n is even
- n is odd
- n has exactly two set bits
- n is a power of two
Show the answer
D. n is a power of two
n - 1 flips the lowest set bit of n and every bit below it, so n & (n - 1) clears the lowest set bit. The result is zero only when n had exactly one set bit: 1, 2, 4, 8 and so on.
This topic is named in a candidate’s interview report: HackWithInfy SP interview, 2022, on GeeksforGeeks.
-
Folders hold files and other folders, nested to any depth. Which data structure models this most naturally?
- A queue
- A stack
- A circular array
- A tree
Show the answer
D. A tree
Each folder is a node whose children are its files and subfolders, under one root. Listing everything inside a folder is a traversal of its subtree. Links that make a file reachable from two places turn the tree into a graph.
This topic is named in a candidate’s interview report: HackWithInfy SP interview, 2022, on GeeksforGeeks.
-
What is the length of the longest common subsequence of "STONE" and "LONGEST"?
- 2
- 3
- 4
- 5
Show the answer
B. 3
dp[i][j] is the answer for the first i letters of one word and the first j of the other: equal letters give dp[i-1][j-1] + 1, otherwise the larger of dp[i-1][j] and dp[i][j-1]. "ONE" appears in order in both words. O(mn) time.
This topic is named in a candidate’s interview report: Specialist Programmer interview, 2026, on GeeksforGeeks.
-
In this grid, 1 is land and 0 is water, and land joins up, down, left and right. How many islands are there?
1 1 0 0 0 1 0 1 1 0 0 1 0 0 1 0- 3
- 5
- 4
- 2
Show the answer
C. 4
The top-left 1s form one island; the two 1s in the right column form another; the lone 1 at row 3, column 1 and the lone 1 at row 4, column 3 are one each: 4. A DFS from each unvisited land cell is O(rows x columns).
This topic is named in a candidate’s interview report: Specialist programmer interview, 2024, on Reddit.
-
Moving only right or down, how many paths lead from the top-left to the bottom-right of a 3 x 4 grid of cells?
- 12
- 7
- 10
- 35
Show the answer
C. 10
Every path makes 2 down moves and 3 right moves in some order: C(5, 2) = 10. The DP version adds the counts from above and from the left, which also handles blocked cells.
This topic is named in a candidate’s interview report: Specialist programmer interview, 2024, on Reddit.
-
In "surrounded regions", every O not connected to the border is turned into X. What is the usual approach?
- Flip every O to X
- Sort the grid
- Flood-fill from each O on the border to mark the safe ones, then flip every unmarked O to X
- Check each O against every border cell
Show the answer
C. Flood-fill from each O on the border to mark the safe ones, then flip every unmarked O to X
Only regions that touch the edge escape. Marking them first with DFS or BFS from the border makes the rest a single pass, O(rows x columns).
This topic is named in a candidate’s interview report: Specialist programmer interview, 2024, on Reddit.
Java 1
-
How long does get(key) usually take on a java.util.HashMap and on a java.util.TreeMap, each with n entries?
- Both O(1)
- Both O(log n)
- HashMap O(log n); TreeMap O(1)
- HashMap O(1) on average; TreeMap O(log n)
Show the answer
D. HashMap O(1) on average; TreeMap O(log n)
HashMap hashes the key straight to a bucket. TreeMap is a red-black tree kept sorted by key, so every get and put costs O(log n), but it can also give the keys in order, the first or last key, or every key in a range.
This topic is named in a candidate’s interview report: Specialist Programmer interview, 2026, on GeeksforGeeks.
Operating systems 2
-
How does a mutex differ from a counting semaphore?
- A mutex lets one holder in at a time and is released by its owner; a semaphore allows up to N and anyone may signal it
- They are two names for the same thing
- A semaphore can only be used between processes
- A mutex lets N threads in at once
Show the answer
A. A mutex lets one holder in at a time and is released by its owner; a semaphore allows up to N and anyone may signal it
A mutex is for mutual exclusion over one resource, owned by the thread that locked it. A counting semaphore counts available slots, such as 5 database connections, and is also used for signalling between threads.
This topic is named in a candidate’s interview report: Specialist programmer interview, 2024, on Reddit.
-
What is the difference between concurrency and parallelism?
- They are the same
- Concurrency always makes a program faster
- Parallelism needs only one core
- Concurrency is dealing with several tasks in overlapping time, possibly by switching on one core; parallelism is actually running tasks at the same instant on several cores
Show the answer
D. Concurrency is dealing with several tasks in overlapping time, possibly by switching on one core; parallelism is actually running tasks at the same instant on several cores
A web server handling many slow connections on one thread is concurrent; splitting a big computation over 8 cores is parallel. Concurrent code needs synchronisation such as mutexes either way.
This topic is named in a candidate’s interview report: Specialist programmer interview, 2024, on Reddit.
Machine learning 1
-
What is chain-of-thought prompting?
- Asking a language model to reason step by step, or showing worked examples with their reasoning, before it gives the answer
- Sending several prompts to different models in a chain
- Training a model on chained documents
- Restricting the model to one-word answers
Show the answer
A. Asking a language model to reason step by step, or showing worked examples with their reasoning, before it gives the answer
Writing out intermediate steps tends to improve multi-step reasoning such as arithmetic. Few-shot prompting is the related idea of putting a handful of example inputs and outputs in the prompt.
This topic is named in a candidate’s interview report: Specialist programmer interview, 2024, on Reddit.