Databricks questions

Cipher Cover

DatabricksOnsiteMedium

A cipher is described by a source string book and a cover: a list of inclusive index intervals [start, end] into book. The cipher text is the pieces of book named by the intervals, joined in the order the intervals are listed. Intervals may be unsorted, may overlap, and may repeat. Every interval satisfies start <= end.

Each test drives one Cipher instance through a sequence of method calls and checks every return value.

Part 1: Read the cipher

Implement two methods.

  • text() returns the current cipher text.
  • locate(k) returns [i, b] for the character at position k (counting from 0) of the cipher text: i is the index in cover of the interval that supplies it, and b is its index in book.
  1. Example 1
    init
    ["abcdefghij",[[1,4],[7,9]]]
    operations
    [["text",[]],["locate",[0]],["locate",[3]],["locate",[4]],["locate",[6]]]
    Output
    ["bcdehij",[0,1],[0,4],[1,7],[1,9]]

    Why: The cover reads indices 1 to 4 (bcde) and then 7 to 9 (hij), so the text is bcdehij. Position 0 is book index 1 of interval 0, position 3 is book index 4 of interval 0, position 4 is book index 7 of interval 1, and position 6 is book index 9 of interval 1.

Constraints

  • 1 <= len(book) <= 10^5
  • 1 <= len(cover) <= 10^3
  • 0 <= cover[i][0] <= cover[i][1] < len(book) for every interval
  • 0 <= k < len(text())
  • 1 <= len(operations) <= 10^4
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question
Cipher Cover: Databricks interview question | LoopCoach