HPE interview prep
18 practice questions on the topics candidates reported from HPE interviews, with answers. The questions are ours; each one links to the interview report that names its topic.
DSA 5
-
Two pointers start at the head of a singly linked list. Each step, one moves one node and the other moves two. When the fast one reaches the end, where is the slow one?
- At the head
- At the middle node
- At the last node
- At the second node
Show the answer
B. At the middle node
The fast pointer covers twice the distance, so the slow one has gone half way. This finds the middle in one traversal; for an even length it lands on the first or second middle depending on the exact loop condition.
This topic is named in a candidate’s interview report: Aruba and R&D intern on-campus interview, 2021, on GeeksforGeeks.
-
Which of these runs in O(log n) time on n items?
- Binary search for a key in a sorted array
- Finding the largest value in an unsorted array
- Comparing every pair of elements
- Printing every element
Show the answer
A. Binary search for a key in a sorted array
Binary search halves what is left on every step, so it needs about log2 n steps: 20 steps for a million items. A scan of every element is O(n), and comparing every pair is O(n^2). n is the size of the input.
This topic is named in a candidate’s interview report: IT developer intern on-campus interview, 2024, on GeeksforGeeks.
-
A stepping number is one whose adjacent digits differ by exactly 1, such as 21 or 123. How many stepping numbers lie between 10 and 30, inclusive?
- 4
- 3
- 5
- 6
Show the answer
A. 4
They are 10, 12, 21 and 23. A BFS or DFS that grows numbers digit by digit (from d, append d - 1 or d + 1) generates them directly instead of testing every number in the range.
This topic is named in a candidate’s interview report: Software development and QA on-campus interview, 2022, on GeeksforGeeks.
-
On an infinite grid you can move one step in any of 8 directions, diagonals included. What is the fewest steps to go from (0, 0) to (3, 4) and then to (1, 1)?
- 7
- 14
- 9
- 6
Show the answer
A. 7
With diagonal moves, the steps between two points are max(|dx|, |dy|), since a diagonal covers one unit of each. (0, 0) to (3, 4) takes 4, and (3, 4) to (1, 1) takes max(2, 3) = 3, so 7 in all.
This topic is named in a candidate’s interview report: Software development and QA on-campus interview, 2022, on GeeksforGeeks.
-
A sorted array may hold negatives, such as [-4, -1, 0, 3, 10]. How do you return the squares in sorted order in O(n)?
- Square every element; the result is already sorted
- Square every element and sort: O(n) on average
- Two pointers at both ends: the larger absolute value gives the next-largest square, written from the back of the result
- Binary search for every square
Show the answer
C. Two pointers at both ends: the larger absolute value gives the next-largest square, written from the back of the result
The largest squares sit at the two ends, so compare |a[left]| with |a[right]|, place the larger square at the end of the output and move that pointer inward. Squaring then sorting works but costs O(n log n). Here the result is [0, 1, 9, 16, 100].
This topic is named in a candidate’s interview report: Software development and QA on-campus interview, 2022, on GeeksforGeeks.
Python 4
-
In a try statement, which block runs whether or not an exception was raised?
- except
- else
- finally
- raise
Show the answer
C. finally
finally always runs, which makes it the place to release resources. except runs only on a matching exception and else only when no exception happened.
This topic is named in a candidate’s interview report: Software development and QA on-campus interview, 2022, on GeeksforGeeks.
-
What does this print?
s = "HPE-2023 is Great!" print("".join(c for c in s if c.islower() or c.isdigit()))- 2023isGreat
- HPE2023isGreat
- 2023isreat
- 2023 is reat
Show the answer
C. 2023isreat
Only lower-case letters and digits survive: capitals (H, P, E, G), the hyphen, the spaces and the ! are all dropped. islower is false for a capital and for anything that is not a letter.
This topic is named in a candidate’s interview report: Software development and QA on-campus interview, 2022, on GeeksforGeeks.
-
How does a Python tuple differ from a list?
- A tuple can only hold numbers
- Lists are always faster
- A tuple is immutable, so it can be a dictionary key or a set member when its items are hashable; a list can change in place
- Tuples cannot be indexed
Show the answer
C. A tuple is immutable, so it can be a dictionary key or a set member when its items are hashable; a list can change in place
Both are ordered and indexable. Because a tuple cannot change, it is hashable (if its contents are) and slightly lighter in memory, which suits fixed records like (x, y). A list suits a collection you append to.
This topic is named in a candidate’s interview report: R&D intern on-campus interview, 2020, on GeeksforGeeks.
-
What does this print?
def set_bits(n): count = 0 while n: n &= n - 1 count += 1 return count print(set_bits(29), set_bits(64))- 3 1
- 5 1
- 4 6
- 4 1
Show the answer
D. 4 1
29 is 11101 in binary, four 1 bits; 64 is 1000000, one. n & (n - 1) clears the lowest set bit, so the loop runs once per 1 bit rather than once per bit (Brian Kernighan's method). Python also has bin(n).count("1") and, from 3.10, n.bit_count().
This topic is named in a candidate’s interview report: R&D intern on-campus interview, 2020, on GeeksforGeeks.
C and C++ 4
-
In a C program, a function declares static int calls = 0; inside it. Where is calls stored?
- On the stack, created fresh on every call
- On the heap, freed when the function returns
- With the globals, in the data area (BSS here, since it starts at zero); it keeps its value between calls
- Only in a CPU register
Show the answer
C. With the globals, in the data area (BSS here, since it starts at zero); it keeps its value between calls
Static and global variables live for the whole run, in the data segment, or BSS when they start at zero. Ordinary local variables sit on the stack and vanish when the function returns. Memory from malloc or new is on the heap until it is freed.
This topic is named in a candidate’s interview report: Aruba and R&D intern on-campus interview, 2021, on GeeksforGeeks.
-
What does this print?
#include <iostream> struct A { A() { std::cout << "A "; } ~A() { std::cout << "~A "; } }; struct B { B() { std::cout << "B "; } ~B() { std::cout << "~B "; } }; struct C : public B, public A { C() { std::cout << "C "; } ~C() { std::cout << "~C "; } }; int main() { C c; }- A B C ~C ~B ~A
- B A C ~B ~A ~C
- C B A ~A ~B ~C
- B A C ~C ~A ~B
Show the answer
D. B A C ~C ~A ~B
Base classes are built in the order they are listed in the class declaration (B, then A), whatever order an initialiser list names them in, and then the class itself. Destruction runs in exactly the reverse order.
This topic is named in a candidate’s interview report: Software development and QA on-campus interview, 2022, on GeeksforGeeks.
-
In C, which of these declarations is an error?
- void *p;
- int *q = (int *)p;
- void f(void);
- void x;
Show the answer
D. void x;
void is an incomplete type with no size, so no variable can have it. A void * is fine: a generic pointer that can hold any object's address, but it must be cast to a real type before you dereference it or do pointer arithmetic.
This topic is named in a candidate’s interview report: Aruba and R&D intern on-campus interview, 2021, on GeeksforGeeks.
-
On a typical 64-bit machine with a 4-byte int, what does this print?
#include <stdio.h> struct S { int i; char c[8]; }; union U { int i; char c[8]; }; int main(void) { printf("%zu %zu\n", sizeof(struct S), sizeof(union U)); return 0; }- 12 12
- 12 8
- 8 8
- 16 8
Show the answer
B. 12 8
A struct gives every member its own space, 4 + 8 = 12, and 12 is already a multiple of int's 4-byte alignment. A union lays all members over the same bytes, so it is as big as its largest member, 8. Writing u.i and then reading u.c reinterprets those same bytes.
This topic is named in a candidate’s interview report: R&D intern on-campus interview, 2020, on GeeksforGeeks.
Operating systems 2
-
What is the key difference between two processes and two threads of one process?
- Processes have separate address spaces; threads of one process share memory
- Threads cannot run at the same time
- Processes share memory; threads do not
- Threads each need their own copy of the program code
Show the answer
A. Processes have separate address spaces; threads of one process share memory
Threads share the process's heap, globals and open files, so they communicate cheaply but need locks. Processes are isolated, so one crashing does not take the other down, and they talk through pipes, sockets or shared memory.
This topic is named in a candidate’s interview report: R&D intern on-campus interview, 2020, on GeeksforGeeks.
-
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: Aruba and R&D intern on-campus interview, 2021, on GeeksforGeeks.
Computer networks 2
-
At which OSI layer does IP routing happen?
- Data link (2)
- Network (3)
- Transport (4)
- Application (7)
Show the answer
B. Network (3)
Routers read IP addresses and forward packets between networks at layer 3. Switches work at layer 2 with MAC addresses.
This topic is named in a candidate’s interview report: Aruba and R&D intern on-campus interview, 2021, on GeeksforGeeks.
-
At which layers of the OSI model does a network interface card (NIC) work?
- The physical and data link layers: it sends the signal and frames data with its MAC address
- Only the application layer
- The transport layer
- The network layer only
Show the answer
A. The physical and data link layers: it sends the signal and frames data with its MAC address
The NIC turns frames into signals on the wire or radio and back, and its burned-in MAC address is the layer 2 identity a switch learns. IP addresses (layer 3) are set by the operating system, not the card.
This topic is named in a candidate’s interview report: Aruba and R&D intern on-campus interview, 2021, on GeeksforGeeks.
Aptitude 1
-
Nine balls look the same, but one is heavier. With a balance scale, what is the fewest number of weighings that always finds the heavy ball?
- 3
- 1
- 2
- 4
Show the answer
C. 2
Weigh 3 against 3. If they balance, the heavy ball is in the other 3; otherwise it is in the heavier group. Then weigh 1 against 1 from that group the same way. Each weighing has 3 outcomes, and 3 x 3 = 9.
This topic is named in a candidate’s interview report: R&D intern on-campus interview, 2020, on GeeksforGeeks.