Nielsen - Intern + PPO | Oncampus Online Assessment
- Posted
- 25 Oct 2026
Summary
I completed an on‑campus online assessment for Nielsen for an internship position.
How it went
Two Coding Problems: Perfect Squares Matching a Pattern & Special Binary Tree Traversal
Topics: Binary Search, Number Theory, Recursion, Binary Search Trees, Tree Traversal, Sorting, Strings.
Problem 1: Find All Perfect Squares Matching a Pattern
Problem Statement
You are given a string pattern consisting of digits (0-9) and underscores (_). Each underscore represents a missing digit.
Find all positive perfect squares that match the given pattern.
A number matches the pattern if:
- Every digit in the pattern matches the corresponding digit in the number.
- Each underscore (
_) can be replaced by any digit from0to9. - The resulting number has exactly the same number of digits as the pattern.
- The resulting number does not begin with
0.
Print all matching perfect squares in ascending order, with each number on a separate line.
Examples
Example 1
Input:
1_0_5
Output:
11025
Explanation:
11025 = 105 * 105, so it is a perfect square and matches the pattern 1_0_5.
Example 2
Input:
12_4_9
Output:
120409
127449
Explanation:
120409 = 347 * 347127449 = 357 * 357
Both numbers match the pattern and are perfect squares. They are printed in ascending order.
Constraints
1 <= pattern.length <= 13patterncontains only digits (0-9) and underscores (_).- The resulting number must have exactly the same number of digits as the pattern.
- The resulting number must not begin with
0. - Zero is not considered a valid square.
- All matching square values fit in a signed 64-bit integer.
Function Signature
vector<long long> findMatchingSquares(string pattern);
Problem 2: Preorder Traversal of a Special Binary Tree
Problem Statement
You are given an array arr containing N integers. Construct a special binary tree using the following steps.
Step 1: Construct the Binary Search Tree
- Sort the array in ascending order.
- Select the middle element as the root:
- If
Nis odd, choose the element at index(N - 1) / 2. - If
Nis even, choose the element at index(N / 2) - 1.
- If
- Recursively construct the left subtree using the elements to the left of the selected middle element.
- Recursively construct the right subtree using the elements to the right of the selected middle element.
- Repeat the same process for both subtrees until all elements have been used.
Step 2: Identify Special Elements
An integer x is called a special element if the sum of the number formed by removing its last digit and the product of that number and the last digit equals the original number.
Let:
last = x % 10remaining = x / 10, using integer division.
The element is special if:
remaining + remaining * last + last == x
Replace every special element in the tree with 0.
Step 3: Return the Preorder Traversal
Perform a preorder traversal of the modified tree and return the resulting sequence.
Preorder traversal visits nodes in this order:
- Root
- Left subtree
- Right subtree
Examples
Example 1
Input:
8
12 8 7 16 19 41 33 29
Output:
16 0 0 12 0 0 33 41
Explanation:
After sorting the array:
7 8 12 16 19 29 33 41
The constructed binary search tree is:
16
/ \
8 29
/ \ / \
7 12 19 33
\
41
The special elements are 7, 8, 19, and 29. Replace these elements with 0.
The preorder traversal of the modified tree is:
16 0 0 12 0 0 33 41
Example 2
Input:
4
1 51 23 49
Output:
23 0 0 51
Explanation:
After sorting the array:
1 23 49 51
The constructed binary search tree is:
23
/ \
1 49
\
51
The special elements are 1 and 49. Replace these elements with 0.
The preorder traversal of the modified tree is:
23 0 0 51
Constraints
1 <= N <= 10001 <= arr[i] <= 1,000,000,000
Function Signature
vector<int> processData(int n, vector<int> arr);
Questions asked (2)
- 01
Find All Perfect Squares Matching a Pattern
Data Structures & Algorithms
You are given a string
patternconsisting of digits (0-9) and underscores (_). Each underscore represents a missing digit.Find all positive perfect squares that match the given pattern. A number matches the pattern if every digit aligns, underscores can be any digit, the length matches, and the number does not start with
0. Output all matching perfect squares in ascending order, one per line.Constraints
1 <= pattern.length <= 13- Digits and underscores only
- Result fits in signed 64‑bit integer
Function Signature:
vector<long long> findMatchingSquares(string pattern); - 02
Preorder Traversal of a Special Binary Tree
Data Structures & Algorithms
Given an array
arrofNintegers, first sort the array and build a binary search tree by repeatedly choosing the middle element as the root (for evenNuse the left‑middle). After the tree is built, replace every special element with0. An elementxis special ifremaining + remaining * last + last == x, wherelast = x % 10andremaining = x / 10(integer division).Return the preorder traversal of the modified tree.
Constraints
1 <= N <= 10001 <= arr[i] <= 1,000,000,000
Function Signature:
vector<int> processData(int n, vector<int> arr);