Katkat

Implement Tail and Find Monster Cost

ConfluentSoftware Engineer · Onsite

Last 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 n lines, 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:

  • S is the start cell.
  • E is the exit cell.
  • . is an empty cell with cost 0.
  • M is a monster cell with cost 1 to 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.