FastPrepCount Level-Order Traversals of a Tree
Problems/Count Level-Order Traversals of a Tree

Count Level-Order Traversals of a Tree

Bank of America logoBank of America● MediumNEW GRADOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported by a Bank of America new grad from a 2023 campus drive, as one of two tasks in a 60-minute online coding test that was followed by a pre-recorded video interview. The problem name attached to the report points to a graph colouring task, but the candidate's described approach counts the ways to traverse a tree whose nodes can have any number of children, using a level-order pass and multiplying the factorials of every node's number of children. This version is built on that approach.

The report gives no statement, input format, examples or limits. Here, the tree comes as a parent array rooted at node 0 with up to 10,000 nodes, and the count is returned modulo 10^9 + 7.

Our version is about 60% the same as the reported question.

A rooted tree has n nodes numbered from 0 to n - 1, and node 0 is the root. You are given an array parent of length n: parent[0] = -1, and for every other node i, parent[i] is the parent of node i. A node may have any number of children.

A level-order traversal visits the nodes with a queue. Start with only the root in the queue. Then, while the queue is not empty, take the node at the front of the queue, visit it, and add all of its children to the back of the queue. The children of one node may be added in any order, so different choices can give different visiting orders.

Return the number of different orders in which a level-order traversal can visit the nodes. The number can be very large, so return it modulo 10^9 + 7.

Function

countLevelOrders(parent: int[]) → int

Examples

Example 1

parent = [-1,0,0,1,1,2,2,2]return = 24

Node 0 has children 1 and 2, node 1 has children 3 and 4, and node 2 has children 5, 6 and 7. These groups can be added in 2, 2 and 6 orders. Every combination of choices gives a different visiting order, so there are 2 * 2 * 6 = 24. One of them is 0, 2, 1, 7, 5, 6, 4, 3: node 2 is visited before node 1, so its children also come before the children of node 1.

Example 2

parent = [-1]return = 1

The tree is only the root, so the one visiting order is 0.

Example 3

parent = [-1,0,0,0,0,0,0,0,0,0,0,0,0,0]return = 227020758

The root has 13 children, which can be added in 13! = 6,227,020,800 orders. Modulo 10^9 + 7 this is 6,227,020,800 - 6 * 1,000,000,007 = 227,020,758.

Constraints

  • 1 <= parent.length <= 10^4
  • parent[0] = -1
  • 0 <= parent[i] < parent.length for every i >= 1
  • Following parents from any node always reaches node 0, so parent describes one tree rooted at node 0.

More Bank of America Online Assessment questions

  • Return Exact Cash Register ChangeOA · Seen Aug 2026
  • Sum Skipping Multiples of 7OA · Seen Nov 2025
  • Wildcard Substring MatchOA · Seen Nov 2025
  • Maximum String BeautyOA · Seen Sep 2024
  • Merge Point of Two Linked ListsOA · Seen Oct 2023
  • Remove Duplicates from a ListOA · Seen Oct 2023
  • Pairs with Target Sum in a Sorted ArrayOA · Seen Oct 2023
  • Count Set BitsOA · Seen Sep 2023
  • Count Pairs with Sum KOA · Seen Jun 2023
  • Letter Pattern SumOA · Seen Sep 2022

Similar questions at other companies

  • Count Distinct Binary Search TreesC3.ai · OAC3.ai interview questions
  • Stable Workload Redistribution TreesTower Research Capital · OA
  • Frog Position After T SecondsInMobi · PHONE SCREENInMobi interview questions
  • Base Unit ConversionsAdobe · OAAdobe interview questions
  • Count Prime Values in SubtreesVisa · OAVisa interview questions
  • Frog Paths Without Long RunsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
  • Minimum N-ary Tree Depth DeletionsSnowflake · OA · PHONE SCREENSnowflake interview questions
  • Tree Pythagorean TriplesIBM · OAIBM interview questions
  • Count LevelsBlackRock · OABlackRock interview questions
  • Count Reachable Values by Halving and DecrementingInfosys · OAInfosys interview questions

Bank of America interview experiences

  • Bank of America interviews · December 2025
  • Bank of America OA · December 2025
  • Bank of America interviews · September 2026
  • Bank of America OA · September 2026
All Bank of America interview questionsSee Bank of America hiring insights
Saved
class Solution {
    public int countLevelOrders(int[] parent) {
        // write your code here
    }
}
Checking account…