PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Meta

Solve word count, node, segmentation, stock tasks

Last updated: Mar 29, 2026

Quick Overview

This multipart question evaluates skills in tokenization and streaming text processing, generic rooted-tree data-structure design and traversal APIs, dictionary-based string segmentation (dynamic programming and prefix/prefix-structure optimization), and constrained algorithmic optimization for stock-trading problems.

  • Medium
  • Meta
  • Coding & Algorithms
  • Software Engineer

Solve word count, node, segmentation, stock tasks

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Onsite

You have four tasks: 1) Count words with explicit rules: Given a text document, return the number of words. First define precisely what qualifies as a “word.” Address contractions like “it’s” (treat as one token by default unless specified), hyphenated compounds, punctuation, numerals, Unicode letters, emojis, and whitespace. State your tokenization policy, then implement an efficient solution that can process very large files in a streaming fashion. Provide time/space complexity and a few test cases. 2) Implement a Node class for a data structure: Design and implement a generic Node class for a rooted tree (each node has an arbitrary number of children). Support operations: addChild, removeChild, moveSubtree(newParent), depth-first traversal (preorder), breadth-first traversal, and find(value) that returns the path from the root. Discuss edge cases (cycles should not be possible; enforce it), error handling, and complexities of each operation. Provide unit tests. 3) String segmentation with a dictionary (inspired): Given a non-empty string s and a set of valid words D, determine whether s can be segmented into a sequence of words from D. If possible, return one valid segmentation; otherwise return an empty result. Optimize for many queries against a fixed D and very long s. Explain and implement your approach (e.g., dynamic programming with prefix structures), plus its time/space complexity. 4) Stock profit with constraints (variant): Given an array prices where prices[i] is the stock price on day i, compute the maximum profit with exactly one buy and one sell, and return the (buyDay, sellDay) indices. Then extend your solution to handle at most two transactions with a mandatory 1-day cooldown between a sell and the next buy, and also support an optional fixed transaction fee f per sell. Provide algorithms, correctness reasoning, and complexities.

Quick Answer: This multipart question evaluates skills in tokenization and streaming text processing, generic rooted-tree data-structure design and traversal APIs, dictionary-based string segmentation (dynamic programming and prefix/prefix-structure optimization), and constrained algorithmic optimization for stock-trading problems.

Related Interview Questions

  • Solve Tree Columns And Maze Variants - Meta (medium)
  • Solve Tree Diameter and Palindromic Counts - Meta (medium)
  • Simulate Monster Team Battles - Meta (hard)
  • Solve a Key-Door Corridor Maze - Meta (medium)
  • Solve Array Merge and Parentheses Cleanup - Meta (medium)
Meta logo
Meta
Sep 6, 2025, 12:00 AM
Software Engineer
Onsite
Coding & Algorithms
2
0

You have four tasks:

  1. Count words with explicit rules: Given a text document, return the number of words. First define precisely what qualifies as a “word.” Address contractions like “it’s” (treat as one token by default unless specified), hyphenated compounds, punctuation, numerals, Unicode letters, emojis, and whitespace. State your tokenization policy, then implement an efficient solution that can process very large files in a streaming fashion. Provide time/space complexity and a few test cases.
  2. Implement a Node class for a data structure: Design and implement a generic Node class for a rooted tree (each node has an arbitrary number of children). Support operations: addChild, removeChild, moveSubtree(newParent), depth-first traversal (preorder), breadth-first traversal, and find(value) that returns the path from the root. Discuss edge cases (cycles should not be possible; enforce it), error handling, and complexities of each operation. Provide unit tests.
  3. String segmentation with a dictionary (inspired): Given a non-empty string s and a set of valid words D, determine whether s can be segmented into a sequence of words from D. If possible, return one valid segmentation; otherwise return an empty result. Optimize for many queries against a fixed D and very long s. Explain and implement your approach (e.g., dynamic programming with prefix structures), plus its time/space complexity.
  4. Stock profit with constraints (variant): Given an array prices where prices[i] is the stock price on day i, compute the maximum profit with exactly one buy and one sell, and return the (buyDay, sellDay) indices. Then extend your solution to handle at most two transactions with a mandatory 1-day cooldown between a sell and the next buy, and also support an optional fixed transaction fee f per sell. Provide algorithms, correctness reasoning, and complexities.

Submit Your Answer

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Meta•More Software Engineer•Meta Software Engineer•Meta Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.