Databricks questions

File Encryption Plan

DatabricksPhone screenHard

You are given a file system tree with n nodes numbered 0 to n - 1. Node 0 is the root directory. parent[i] is the parent of node i, with parent[0] = -1 and parent[i] < i for i > 0. kind[i] is 0 for a directory and 1 for a file; files are leaves. encrypted[i] is true when file i is already encrypted. Directories are never marked encrypted.

Two kinds of calls encrypt files: a call on a file encrypts that one file, and a call on a directory encrypts every file in that directory's subtree at once. Build an EncryptionPlan object that answers queries about the cheapest way to get every file encrypted. Each test creates one instance and checks every method's return value.

Part 1: Cheapest time

Implement counts() and min_time(request, per_file).

counts() returns [encrypted, unencrypted]: the number of files already encrypted, then the number of files not encrypted.

min_time(request, per_file) returns the least total time needed to encrypt every file that is not yet encrypted. A call on one file takes request + per_file. A call on a directory takes request + per_file * f, where f is the total number of files in that directory's subtree, counting files that are already encrypted. A directory call encrypts every file in its subtree, so no further calls are needed below it. If no file needs encryption, return 0.

  1. Example 1
    init
    [[-1,0,0,1,1],[0,0,1,1,1],[false,false,false,false,true]]
    operations
    [["counts",[]],["min_time",[5,2]],["min_time",[1,5]],["min_time",[100,1]]]
    Output
    [[1,2],11,12,103]

    Why: Node 4 is the only encrypted file, so counts() is [1, 2]. With request = 5, per_file = 2, a call on the root costs 5 + 3 * 2 = 11 because 3 files lie below it, beating two single-file calls at 7 each. With request = 1, per_file = 5, the two single-file calls cost 6 each, 12 in total, while the root call costs 16. With request = 100, per_file = 1, one root call costs 103, cheaper than 202 for two file calls.

Constraints

  • 1 <= len(parent) <= 10^5
  • parent[0] = -1
  • 0 <= parent[i] < i for i > 0
  • kind[i] is 0 (directory) or 1 (file)
  • node 0 is a directory and files have no children
  • 0 <= request <= 10^9
  • 0 <= per_file <= 10^9
  • 1 <= len(operations) <= 10
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question