Skip to content

Instantly share code, notes, and snippets.

@blackwatertepes
Last active April 22, 2022 19:20
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 blackwatertepes/0da142e55e0010a088308ed50ad1c10f to your computer and use it in GitHub Desktop.
Save blackwatertepes/0da142e55e0010a088308ed50ad1c10f to your computer and use it in GitHub Desktop.
Search Units - Code Templates
Unit Search Problem
You work for a hospitality platform where guests can book units, and we want to give guests the ability to search for units, based on their chosen location. Your task is return a list of units, that meet the search criteria provided by the user.
Code organized cleanly (Separation of Concerns and/or Object-Oriented); ex: a Unit Class
Implement a filter
Implement a sort, w/ a Comparator
Handle unexpected input
Understand the difference between +/- vs *//
Understand the concept of normalization
Understand the concept of weighting
What the candidate did well...
What the candidate didn't do well...
What the candidate could improve on...
// FINISH ME
type Unit struct {
Id int
Location Location
UserRating float64
PriceInCents int
}
type Location struct {
Lat float64
Lng float64
}
class Unit {
int id;
double lat;
double lng;
double rating;
int price;
public Unit(String[] args) {
id = Integer.parseInt(args[0]);
lat = Double.parseDouble(args[1]);
lng = Double.parseDouble(args[2]);
rating = Double.parseDouble(args[3]);
price = Integer.parseInt(args[4]);
}
}
public double distanceBetween(double[] locationA, double[] locationB) {
return Math.sqrt(Math.pow(locationA[0] - locationB[0], 2) + Math.pow(locationA[1] - locationB[1], 2));
}
class UnitComparator implements Comparator<Unit> {
@Override
public int compare(Unit unitA, Unit unitB) {
return unitA.value > unitB.value ? -1 : unitA.value == unitB.value ? 0 : 1;
}
}
/**
* Constructs a Unit from a unit, which is an array of strings.
*/
class Unit {
constructor(args) {
this.id = parseInt(args[0])
this.lat = parseFloat(args[1])
this.lng = parseFloat(args[2])
this.rating = parseFloat(args[3])
this.price = parseInt(args[4])
}
}
/**
* Calculates the distance between two locations.
* Locations are 2-place arrays, consisting of lat, lng.
*/
function distanceBetween(locationA, locationB) {
return Math.sqrt(Math.pow(locationA[0] - locationB[0], 2) + Math.pow(locationA[1] - locationB[1], 2));
}
class Unit(
val stringParams: MutableList<String>
) {
var id: Int
var lat: Double
var lng: Double
var rating: Double
var price: Int
init {
id = stringParams[0].toInt()
lat = stringParams[1].toDouble()
lng = stringParams[2].toDouble()
rating = stringParams[3].toDouble()
price = stringParams[4].toInt()
}
}
fun distanceBetween(locationA: MutableList<Double>, locationB: MutableList<Double>): Double {
val location1Diff = locationA[0] - locationB[0]
val location2Diff = locationA[1] - locationB[1]
return Math.pow(Math.pow(location1Diff, 2.0) + Math.pow(location2Diff, 2.0), 0.5)
}
class Unit:
def __init__(self, args):
self.id = int(args[0])
self.lat = float(args[1])
self.lng = float(args[2])
self.rating = float(args[3])
self.price = int(args[4])
def distanceBetween(locationA, locationB):
return math.sqrt((locationA[0] - locationB[0])**2 + (locationA[1] - locationB[1])**2)
class Unit
attr_reader :id, :lat, :lng, :rating, :price
def initialize(args)
@id = args[0].to_i
@lat = args[1].to_f
@lng = args[2].to_f
@rating = args[3].to_f
@price = args[4].to_i
end
end
def distanceBetween(locationA, locationB)
return Math.sqrt((locationA[0] - locationB[0])**2 + (locationA[1] - locationB[1])**2)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment