2021/09/30~2021/10/06【Senior Backend Engineer】未錄取
公司規模約 50 人,其中約 30 位工程師。公司主要產品是在東南亞提供廉價網路服務。
本關約進行半小時 Culture Fit 審核。
總共 2 題 HackerRank 題目,時間限制 120 分鐘,第二題限定使用 Java。
A single roll operation on a string is a circular increment of each character by one. Looking at the English alphabet, characters in the range ascii[a-z], a becomes b, b becomes c, and z becomes a.
Given an array of integers named roll, perform a roll operation on the first roll[i] characters of s for each element i in the array. Given a zero indexed string, an operation roll[i] affects characters at positions 0 through (roll[i]-1).
Example
s = 'abz'
roll = [3, 2, 1]
Perform the following sequence of operations:
After performing the operations, the final value of s is 'dda'.
Function Description
Complete the function rollTheString in the editor below.
rollTheString has the following parameter(s):
Returns:
Constraints
The area and perimeter of different geometric shapes like rectangle, circle, or square are calculated using different mathematical formulae.
Create the following three classes:
The Rectangle class should implement the Shape interface. It should have two class variables of float type, length and, width. Also, it should implement the following methods:
Note: {length} and {width} respectively represent the class variables length and width. {area} and {perimeter} respectively represent the area and perimeter of the rectangle. For example, given that length = 2 and width = 3, calling the method toString() will return "Rectangle = [length: 2.0, width: 3.0, area: 6.0, perimeter: 10.0]".
The Square class that should inherit the Rectangle class and should implement the following methods:
Note: {length} represents the variable length of Rectangle class. {area} and {perimeter} respectively represent the area and perimeter of the square. For example, given that side = 2, calling the method toString() will return "Square = [side: 2.0, area: 4.0, perimeter: 8.0]".
The Circle class that implements the Shape interface. It should have a class variable, radius. Also, it should implement the following methods:
Note: {radius} represents the variable radius of Circle class. {area} and {perimeter} respectively represent the area and perimeter of the circle. For example, given that radius = 1, calling the method toString() will return "Circle = [radius: 1.0, area: 3.14, perimeter: 6.28]".
The locked stub code in the editor provides the definition of the Shape interface. It also validates the implementation of Rectangle, Square, and Circle classes.
Constraints
人資在照會當下並未說明技術前測有限定語言,因此在無預警情況下被迫使用 Java 進行 HackerRank 測驗,並於測驗隔日隨即收到感謝信。個人認為人資非常失職,甚至覺得被洗了業績,詢問後未得到回覆,整體體驗非常糟糕且差勁,希望欲投此間公司的朋友慎重考量。
以下解法為面試當下所寫,並非最優解。
#!/bin/python3
import mathimport osimport randomimport reimport sys
## Complete the 'rollTheString' function below.## The function is expected to return a STRING.# The function accepts following parameters:# 1. STRING s# 2. INTEGER_ARRAY roll#
def rollTheString(s, rolls): n = len(s) roll_counts = [0] * n
alphbets = 'abcdefghijklmnopqrstuvwxyz' alphbet_map = {a: i for i, a in enumerate(alphbets)}
for roll in rolls: for i in range(roll): roll_counts[i] += 1
results = [] for i, roll_count in enumerate(roll_counts): original_alphabet = s[i] k = alphbet_map[original_alphabet] rolled_alphabet = alphbets[(k + roll_count) % 26] results.append(rolled_alphabet)
return ''.join(results)
if __name__ == '__main__': # ...
參考 Roll the characters of string - GeeksforGeeksopen_in_new 之後重新整理出時間複雜度為 的解法:
def rollTheString(s, rolls): s_len = len(s) rolls_len = len(rolls) roll_counts = [0] * s_len
for roll in rolls: roll_counts[roll - 1] += 1
for i in range(rolls_len - 2, -1, -1): roll_counts[i] += roll_counts[i + 1]
alphbets = 'abcdefghijklmnopqrstuvwxyz' alphbet_map = {a: i for i, a in enumerate(alphbets)}
results = [] for i, roll_count in enumerate(roll_counts): original_alphabet = s[i] k = alphbet_map[original_alphabet] rolled_alphabet = alphbets[(k + roll_count) % 26] results.append(rolled_alphabet)
return ''.join(results)
assert rollTheString('a', [1]) == 'b'assert rollTheString('aa', [1, 1]) == 'ca'assert rollTheString('bca', [1, 2, 3]) == 'eeb'assert rollTheString('zcza', [1, 1, 3, 4]) == 'debb'
import java.util.Scanner;
interface Shape { float getArea(); float getPerimeter(); String toString();}
/* * Write the implementations of the Rectangle, Square, and Circle classes. */
public class Solution { private static final Scanner INPUT_READER = new Scanner(System.in);
public static void main(String[] args) { String[] lengthWidth = INPUT_READER.nextLine().split(" "); float length = Float.parseFloat(lengthWidth[0]); float width = Float.parseFloat(lengthWidth[1]);
float side = Float.parseFloat(INPUT_READER.nextLine()); float radius = Float.parseFloat(INPUT_READER.nextLine());
Shape rectangle = new Rectangle(length, width); Rectangle square = new Square(side); Shape circle = new Circle(radius);
System.out.println("===================================="); System.out.println("Finding area and perimeter of shapes"); System.out.println("====================================");
System.out.println("Area = " + rectangle.getArea() + " and Perimeter = " + rectangle.getPerimeter() + "\n"); System.out.println("Area = " + square.getArea() + " and Perimeter = " + square.getPerimeter() + "\n"); System.out.println("Area = " + circle.getArea() + " and Perimeter = " + circle.getPerimeter() + "\n");
System.out.println("========================="); System.out.println("Printing shapes as string"); System.out.println("=========================");
System.out.println(rectangle.toString()); System.out.println(square.toString()); System.out.println(circle.toString()); }
public static class Rectangle implements Shape { float length; float width;
public Rectangle(float new_length, float new_width) { this.length = new_length; this.width = new_width;
}
@Override public float getArea() { System.out.println("Finding area of rectangle with length = " + this.length + " and width = " + this.width); return this._getArea(); }
@Override public float getPerimeter() { System.out.println("Finding perimeter of rectangle with length = " + this.length + " and width = " + this.width); return _getPerimeter(); }
@Override public String toString() { return "Rectangle = [length: " + this.length + ", width: " + this.width + ", area: " + _getArea() + ", perimeter: " + _getPerimeter() + "]"; }
protected float _getArea() { return this.length * this.width; }
protected float _getPerimeter() { return (this.length + this.width) * 2; } }
public static class Square extends Rectangle { public Square(float side) { super(side, side); }
@Override public float getArea() { System.out.println("Finding area of square with side = " + this.length); return super.getArea(); }
@Override public float getPerimeter() { System.out.println("Finding perimeter of square with side = " + this.length); return super.getPerimeter(); }
@Override public String toString() { return "Square = [side: " + this.length + ", area: " + _getArea() + ", perimeter: " + _getPerimeter() + "]"; } }
public static class Circle implements Shape { float radius;
public Square(float new_radius) { this.radius = new_radius; }
@Override public float getArea() { System.out.println("Finding area of circle with radius = " + this.radius); return this._getArea(); }
@Override public float getPerimeter() { System.out.println("Finding perimeter of circle with radius = " + this.radius); return _getPerimeter(); }
@Override public String toString() { return "Circle = [radius: " + this.radius + ", area: " + _getArea() + ", perimeter: " + _getPerimeter() + "]"; }
protected float _getArea() { return (float)3.14 * this.radius * this.radius; }
protected float _getPerimeter() { return (float)6.28 * this.radius; } }