FastPrepChristmas Tree with N Tiers
Problems/Christmas Tree with N Tiers

Christmas Tree with N Tiers

Verily logoVerily● EasyFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Verily onsite coding round, written by hand without AI tools. The candidate had to print a Christmas tree whose number of tiers is the input, and describes the round as a test of careful string building and counting spaces.

The report does not describe the tree's shape. We chose the shape ourselves for practice: tier k has k + 1 centred rows that grow by two stars, a one-character trunk ends the tree, and the rows are returned as a list instead of printed. Trees have at most 20 tiers.

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

Draw a Christmas tree with n tiers out of text, and return its rows from top to bottom as an array of strings. The tree follows these rules:

  • The tree is 2n + 1 characters wide, and every row is centred on the middle column, which is column n when columns are numbered from 0.
  • Tier k, for k = 1, 2, ..., n from the top, has k + 1 rows with 1, 3, 5, ..., 2k + 1 stars. Row j of a tier, counting from 0, is n - j spaces followed by 2j + 1 copies of '*'.
  • Below the last tier is the trunk: one row of n spaces followed by '|'.
  • No row has spaces after its last character.

Printing the tree would output these strings one per line; here they are returned instead.

Function

christmasTree(n: int) → String[]

Examples

Example 1

n = 1return = [" *","***"," |"]
  • The tree is 3 columns wide. The single tier has 2 rows with 1 and 3 stars.
  • The trunk | sits in the middle column, after 1 space.

Example 2

n = 2return = ["  *"," ***","  *"," ***","*****","  |"]
  • The tree is 5 columns wide, so the middle column has 2 spaces before it.
  • Tier 1 has 2 rows (1 and 3 stars), tier 2 has 3 rows (1, 3 and 5 stars), then the trunk.

Example 3

n = 3return = ["   *","  ***","   *","  ***"," *****","   *","  ***"," *****","*******","   |"]
  • The tree is 7 columns wide. Tiers 1, 2 and 3 have 2, 3 and 4 rows, so there are 9 star rows plus the trunk.
  • No row has spaces after its last character.

Constraints

  • 1 <= n <= 20

More Verily Onsite Interview questions

  • Confusing Bib NumbersONSITE INTERVIEW · Seen Nov 2026
  • Package Install OrderONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Bounded Word Guess SolverAtlassian · ONSITE INTERVIEWAtlassian interview questions
  • Document Edit EventsClickUp · ONSITE INTERVIEW
  • Predict Runtime Method DispatchAMD · ONSITE INTERVIEWAMD interview questions
  • Basic String Compression with Length GuardNew Relic · PHONE SCREEN · ONSITE INTERVIEW
  • Print Sentences as TableAirbnb · PHONE SCREEN · ONSITE INTERVIEWAirbnb interview questions
  • Retry with an Attempt LimitOracle · ONSITE INTERVIEWOracle interview questions
  • Extract Error Log ContextFlexTrade · ONSITE INTERVIEWFlexTrade interview questions
  • Run-Length Encode Consecutive Letter RunsMorgan Stanley · ONSITE INTERVIEWMorgan Stanley interview questions
  • String Pattern ReplacementGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
  • IPv4 Forward IteratorOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions

Interview reports that mention this question

  • Verily SWE Onsite Interview Questions: 1 Experience
See Verily hiring insights
Saved
class Solution {
    public String[] christmasTree(int n) {
        // Write your code here.
    }
}
Checking account…