Skip to content

Instantly share code, notes, and snippets.

@nojvek
Last active February 4, 2019 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nojvek/4a544a304f2fd161d5b020f7ab35fdcf to your computer and use it in GitHub Desktop.
Save nojvek/4a544a304f2fd161d5b020f7ab35fdcf to your computer and use it in GitHub Desktop.
Bundler interview question
/**
* **********
* ** TIPS **
* **********
* Please write high level pseudocode approach before writing actual code.
* Use meaningful variables, comments and indent your code.
* Your code should compile, run and produce the correct output.
* Don't need to test extensively but describe what kind of tests you'll add as 'TODO:' comments
* You are totally free to use google and stackoverflow.
* Talk about O(n) CPU and memory requirements. Best case and worst case.
* Talk about what things you'll still need to do for your code to be production ready at the end.
*/
//// Javascript ////
/**
* Large JS applications need to bundle their javascript in a single file to reduce network trips.
* However since the application is written in many little modules and libraries, we need a smart way of bundling them
* so we only include the code that is needed to load the application.
* Write a bundling algorithm, that takes in an entry module.
* It prints the bundle string with base dependencies first and entry module content last.
*/
class Bundler {
// Adds module, its dependencies and its code to an internal store
addModule(name, deps, content) {
// TODO: complete me
console.log(name, deps, content)
}
// Prints bundle text where dependencies are loaded first
// entryModule is always loaded last and all content is separated by a new line
computeBundle(entryModule) {
// TODO: complete me
console.log("<bundle output here>")
}
}
const bundler = new Bundler()
bundler.addModule("a", ["b", "c"], "a(){}")
bundler.addModule("b", ["e", "c"], "b(){}")
bundler.addModule("c", ["d"], "c(){}")
bundler.addModule("d", ["e"], "d(){}")
bundler.addModule("e", [], "e(){}")
bundler.addModule("f", ["g"], "f(){}")
bundler.addModule("g", [], "g(){}")
/* Expected:
e(){}
d(){}
c(){}
b(){}
a(){}
*/
bundler.computeBundle("a")
//// Python ////
###
# Large JS applications need to bundle their javascript in a single file to reduce network trips.
# However since the application is written in many little modules and libraries, we need a smart way of bundling them
# so we only include the code that is needed to load the application.
# Write a bundling algorithm, that takes in an entry module.
# It prints the bundle string with base dependencies first and entry module content last.
###
class Bundler(object):
# Adds module, its dependencies and its code to an internal store
def add_module(self, name, deps, content):
# TODO: complete me
print(name, deps, content)
# Prints bundle text where dependencies are loaded first
# entry_module is always loaded last and all content is separated by a new line
def compute_bundle(self, entry_module):
# TODO: complete me
print("<bundle output here>")
bundler = Bundler()
bundler.add_module("a", ["b", "c"], "a(){}")
bundler.add_module("b", ["e", "c"], "b(){}")
bundler.add_module("c", ["d"], "c(){}")
bundler.add_module("d", ["e"], "d(){}")
bundler.add_module("e", [], "e(){}")
bundler.add_module("f", ["g"], "f(){}")
bundler.add_module("g", [], "g(){}")
# Expected:
# e(){}
# d(){}
# c(){}
# b(){}
# a(){}
bundler.compute_bundle("a")
//// Java ////
import java.util.*;
/**
* Large JS applications need to bundle their javascript in a single file to reduce network trips.
* However since the application is written in many little modules and libraries, we need a smart way of bundling them
* so we only include the code that is needed to load the application.
* Write a bundling algorithm, that takes in an entry module.
* It prints the bundle string with base dependencies first and entry module content last.
*/
class Bundler {
// Adds module, its dependencies and its code to an internal store
public void addModule(String name, List<String>deps, String content) {
// TODO: complete me
System.out.println("Adding module: " + name);
}
// Prints bundle text where dependencies are loaded first
// entryModule is always loaded last and all content is separated by a new line
public void computeBundle(String entryModule) {
// TODO: complete me
System.out.println("TODO: <bundle output here>");
}
}
class Solution {
public static void main(String[] args) {
Bundler bundler = new Bundler();
bundler.addModule("a", Arrays.asList("b", "c"), "a(){}");
bundler.addModule("b", Arrays.asList("e", "c"), "b(){}");
bundler.addModule("c", Arrays.asList("d"), "c(){}");
bundler.addModule("d", Arrays.asList("e"), "d(){}");
bundler.addModule("e", Arrays.asList(), "e(){}");
bundler.addModule("f", Arrays.asList("g"), "f(){}");
bundler.addModule("g", Arrays.asList(), "g(){}");
/* Expected:
e(){}
d(){}
c(){}
b(){}
a(){}
*/
bundler.computeBundle("a");
}
}
//// C# ////
/**
* Large JS applications need to bundle their javascript in a single file to reduce network trips.
* However since the application is written in many little modules and libraries, we need a smart way of bundling them
* so we only include the code that is needed to load the application.
* Write a bundling algorithm, that takes in an entry module.
* It prints the bundle string with base dependencies first and entry module content last.
*/
using System;
public class Bundler {
// Adds module, its dependencies and its code to an internal store
public void AddModule(string name, string[] deps, string content) {
// TODO: complete me
Console.WriteLine(name);
}
// Prints bundle text where dependencies are loaded first
// entryModule is always loaded last
public void ComputeBundle(string entryModule) {
// TODO: complete me
Console.WriteLine("<bundle output here>");
}
}
class Solution {
static void Main(string[] args) {
var bundler = new Bundler();
bundler.AddModule("a", new string[] {"b", "c"}, "a(){}");
bundler.AddModule("b", new string[] {"e", "c"}, "b(){}");
bundler.AddModule("c", new string[] {"d"}, "c(){}");
bundler.AddModule("d", new string[] {"e"}, "d(){}");
bundler.AddModule("e", new string[] {}, "e(){}");
bundler.AddModule("f", new string[] {"g"}, "f(){}");
bundler.AddModule("g", new string[] {}, "g(){}");
/* Expected:
e(){}
d(){}
c(){}
b(){}
a(){}
*/
bundler.ComputeBundle("a");
}
}
//// SOLN ////
from collections import OrderedDict
class Bundler(object):
def __init__(self):
self.modules = {}
# Adds module, its dependencies and its code to an internal store
def add_module(self, name, deps, content):
self.modules[name] = {'deps': deps, 'content': content}
# Returns bundle text where dependencies are loaded first
# entry_module is always loaded last and all content is separated by a new line
def compute_bundle(self, entry_module):
# Create set to hold module's full dependencies
deps_set = OrderedDict()
self.compute_deps(entry_module, deps_set)
return '\n'.join(map(lambda dep: self.modules[dep]['content'], deps_set.keys()))
def compute_deps(self, module, deps_set):
module_deps = self.modules[module]['deps']
# Only compute depencies of modules that we haven't seen yet
for dep in module_deps:
if dep not in deps_set:
self.compute_deps(dep, deps_set)
deps_set[module] = True
bundler = Bundler()
bundler.add_module("b", ["e", "c"], "b(){}")
bundler.add_module("a", ["b", "c"], "a(){}")
bundler.add_module("c", ["d"], "c(){}")
bundler.add_module("d", ["e"], "d(){}")
bundler.add_module("e", [], "e(){}")
bundler.add_module("f", ["g"], "f(){}")
bundler.add_module("g", [], "g(){}")
print(bundler.compute_bundle("a"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment