Caterpillar interview prep
12 practice questions on the topics candidates reported from Caterpillar interviews, with answers. The questions are ours; each one links to the interview report that names its topic.
Practise them as a timed round8 roles open at Caterpillar Inc.
DSA 5
-
What is the length of the longest run of consecutive integers in [100, 4, 200, 1, 3, 2], in any order?
- 3
- 4
- 6
- 2
Show the answer
B. 4
1, 2, 3 and 4 are all present. Put every number in a hash set, start counting only from numbers whose predecessor is missing, and each number is visited a constant number of times: O(n).
This topic is named in a candidate’s interview report: Software engineer intern and full-time interview, 2023, on LeetCode.
-
What is the smallest positive integer missing from [3, 4, -1, 1]?
- 5
- 0
- 2
- 1
Show the answer
C. 2
1, 3 and 4 are present and 2 is not. The O(n), O(1)-space method swaps each value v in 1..n to index v - 1, then the first index i holding the wrong value gives i + 1.
This topic is named in a candidate’s interview report: Software engineer intern and full-time interview, 2023, on LeetCode.
-
Which data structure allows insertion and deletion at both ends in O(1)?
- A stack
- A binary search tree
- A deque (double-ended queue)
- A priority queue
Show the answer
C. A deque (double-ended queue)
A deque supports push and pop at the front and the back. Python's collections.deque, C++'s std::deque and Java's ArrayDeque provide it, and a deque is what sliding-window maximum algorithms keep their candidates in.
This topic is named in a candidate’s interview report: SDE intern on-campus interview, 2022, on Code360.
-
What is the largest distance (difference of indexes) between two equal elements in [3, 2, 1, 2, 1, 4, 5, 8, 6, 7, 4, 2]?
- 10
- 5
- 9
- 8
Show the answer
A. 10
The 2s sit at indexes 1 and 11. Store the first index of each value in a hash map and, at every later occurrence, compare the current index minus the stored one: one pass, O(n).
This topic is named in a candidate’s interview report: SDE intern on-campus interview, 2022, on Code360.
-
You remove the 2nd node from the end of 1 -> 2 -> 3 -> 4 -> 5 in one pass with two pointers. What is the list after?
- 1 -> 2 -> 3 -> 5
- 1 -> 2 -> 4 -> 5
- 1 -> 3 -> 4 -> 5
- 1 -> 2 -> 3 -> 4
Show the answer
A. 1 -> 2 -> 3 -> 5
Move a fast pointer n steps ahead, then move both until fast reaches the last node: slow then sits just before the node to remove, 3 here, and slow.next = slow.next.next drops 4. A dummy node before the head handles removing the head itself.
This topic is named in a candidate’s interview report: SDE intern on-campus interview, 2022, on Code360.
Python 1
-
b is a with one extra character added and the letters shuffled. What does this print?
from functools import reduce a, b = "intern", "nidtern" print(chr(reduce(lambda x, c: x ^ ord(c), a + b, 0)))- n
- e
- d
- i
Show the answer
C. d
XOR-ing every character code of both strings cancels each pair, because x ^ x = 0, even the repeated n, and leaves the code of the extra character, d. A Counter difference finds it too, with O(alphabet) extra space.
This topic is named in a candidate’s interview report: SDE intern on-campus interview, 2022, on Code360.
C and C++ 2
-
On a typical machine with a 32-bit int, what does this print?
#include <stdio.h> #include <limits.h> int main(void) { printf("%d %d\n", INT_MIN, INT_MAX); return 0; }- -32768 32767
- -2147483647 2147483647
- 0 4294967295
- -2147483648 2147483647
Show the answer
D. -2147483648 2147483647
A 32-bit two's complement int holds -2^31 to 2^31 - 1. The C standard only promises at least 16 bits, so portable code reads INT_MIN and INT_MAX from limits.h, or uses int32_t from stdint.h when it needs an exact width.
This topic is named in a candidate’s interview report: SDE intern on-campus interview, 2022, on Code360.
-
What does this print?
#include <stdio.h> int main(void) { int a[] = {10, 20, 30}; printf("%d %d\n", *(a + 1), *a + 1); return 0; }- 20 11
- 11 11
- 20 21
- 11 20
Show the answer
A. 20 11
a + 1 points at the second element, so *(a + 1) is a[1], 20. In *a + 1 the * binds first: a[0] is 10, plus 1 is 11. a[i] is defined as *(a + i).
This topic is named in a candidate’s interview report: SDE intern on-campus interview, 2022, on Code360.
DBMS and SQL 1
-
An email column holds "rohit.k@example.com". In SQLite, what does this return?
SELECT SUBSTR(email, 1, INSTR(email, '@') - 1) FROM users;- rohit.k@
- rohit
- example.com
- rohit.k
Show the answer
D. rohit.k
INSTR gives the 1-based position of "@", 8 here, so the substring takes the 7 characters before it. MySQL has SUBSTRING_INDEX(email, '@', 1) for the same job.
This topic is named in a candidate’s interview report: SDE intern on-campus interview, 2022, on Code360.
Machine learning 1
-
In TF-IDF, why is a word that appears in nearly every document given a low weight?
- Because TF-IDF ignores word counts
- Because TF is always 1
- Because common words are removed before counting
- IDF is log(N / documents containing the word), which is near 0 for such a word, so it cannot tell documents apart
Show the answer
D. IDF is log(N / documents containing the word), which is near 0 for such a word, so it cannot tell documents apart
TF rewards a word that is frequent in this document; IDF discounts words frequent everywhere, like "the". Their product highlights words that are characteristic of one document, which is why TF-IDF vectors work well for search and text classification.
This topic is named in a candidate’s interview report: SDE intern on-campus interview, 2022, on Code360.
Aptitude 2
-
A father is three times as old as his son. In 8 years he will be two and a half times as old. In 16 years, how many times the son's age will he be?
- 2.2
- 2
- 2.5
- 1.8
Show the answer
A. 2.2
With the son at s, 3s + 8 = 2.5(s + 8), so 0.5s = 12 and s = 24, father 72. In 16 years they are 40 and 88, and 88 / 40 = 2.2.
This topic is named in a candidate’s interview report: SDE intern on-campus interview, 2022, on Code360.
-
Sales fell by 80% from one year to the next, then returned to the first year's level the year after. By what percentage did they rise in that last year?
- 80%
- 500%
- 400%
- 100%
Show the answer
C. 400%
From 100 the sales drop to 20, then climb back to 100: a rise of 80 on a base of 20, which is 400%. A fall and a rise of the same percentage never cancel, because the base changes.
This topic is named in a candidate’s interview report: SDE intern on-campus interview, 2022, on Code360.