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.