Implement Tail and Find Monster Cost
ConfluentSoftware Engineer · OnsiteLast updated
This question evaluates competency in efficient file I/O and streaming techniques for implementing tail, and in graph search and shortest-path algorithms with path reconstruction for computing minimum monster cost in a grid.
This entry contains two coding tasks from the same interview category.
Task A: Implement a file tail operation
Implement a function tail(filePath, n) that returns the last n lines of a text file in their original order.
Requirements:
- The input is a real file path, not an in-memory string. You should be comfortable creating a small text file as a test case and reading it back.
- If the file has fewer than
nlines, return all lines. - If
n == 0, return an empty result. - A trailing newline at the end of the file should not create an extra empty line.
- Discuss or implement an approach that does not load the entire file into memory when the file is very large.
Task B: Find minimum monster cost in a grid
You are given a 2D grid representing a dungeon:
Sis the start cell.Eis the exit cell..is an empty cell with cost0.Mis a monster cell with cost1to enter.#is a wall that cannot be entered.
From any cell, you may move up, down, left, or right. Return the minimum total monster cost required to travel from S to E. If the exit is unreachable, return -1.
Follow-ups:
- Return one path that achieves the minimum cost.
- Extend the grid so each monster cell has an arbitrary non-negative integer cost instead of cost
1.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.