38

No one but the folks who reviewed your code for the company can say with certainty why you were rejected. It also depends on the position you're applying for. If you're applying for an entry-level role or a guru role, the expectations are drastically different. As a hiring manager, a couple things jumped out at me (from most to least relevant to me, a ...


26

This is mostly how I would solve it (in Python): In [2]: import random In [3]: class Deck: ...: def __init__(self): ...: self._cards = list(range(52)) ...: def shuffle(self): ...: random.shuffle(self._cards) ...: def deal(self): ...: return self._cards.pop() The data structure is as simple as it gets, ...


25

While not strictly required, we value usage instructions, nicely-modeled data, automated tests, and thoughtful consideration of architectural decisions and simplicity-vs-completeness trade-offs. Given this clause, a few things stand out from your code: TypeScript - TypeScript is overkill for this problem. TypeScript is a fine language but not all projects ...


13

To be using strict null checks, you must be on "typescript": "^2.0.0". Thus, my example will be using this version of typescript with "compilerOptions":{"strictNullChecks": true}. It does seem that the filter solution is the most elegant solution to the titular question. Sadly, it seems that the typescript static analysis is unable to track this behavior: ...


11

One edge case you're missing is how to handle null. isEmpty(null) will fail because Object.getOwnPropertyNames(null) will throw TypeError: can't convert null to object. Also be aware that isEmpty(NaN) will return false, which may or may not be what you want.


10

I sympathise :) I'm not sure if my answer will actually help much as it's either a rant or a list of hard work to do but hopefully it informs. Interview tests are hopelessly flawed experiments When we give someone a test in an interview, what are we testing? If it's something along the lines of "this person is a good programmer", and we're testing a pool ...


9

Typescript 1.6 introduced user defined type guards that are a function whose return type is a boolean that is cast as a type predicate. To define a simple isString type guard (note the X is string return type), use syntax like below: function isString(str: string | undefined): str is string { return str !== undefined } or as a lambda with a slightly ...


7

A few of thoughts: You don't need to declare types for the arguments in the implementation of your constructor, just the overloads (which precede the implementation). I wouldn't use a separate object for the height and width, why not declare them as private members? Consider throwing TypeError instead of plain Error if your constructor can't figure the ...


7

Your service looks perfectly fine to me. As far as the Clock component goes, I can only recommend one thing. When getClock().subscribe(...) is executed, a reference to the subscription is being returned back. Ideally, the reference should be kept as long as the component is alive. On component destruction, the subscription should be released by invoking ...


6

Edit: I've updated the code and uploaded a gist that will work in the TS playground - the gist will compile with Typescript 3.1. The primary benefit of Typescript is to provide types for Javascript. And since Typescript provides Generics - I don't think your implementation of a Dictionary provides any benefit over a plain javascript object. In fact, as you ...


6

Dead code interface NaturalSortConfig { keyFunc?: NaturalKeyFunc; //desc?: boolean; //caseInsensitive?: boolean; } Dead code does nothing, except take up screen space, and confuse people who don't know why it was written and removed in the first place. Braces This is a bit of a holy-war. However, at least one bug (the Apple SSL bug) was ...


6

You could move the door.locked check into the door.unlock() function (or into a new function) which would allow you return a promise for both cases: function unlockDoor(door) { return door.locked ? door.unlock() : Promise.resolve(); } } function openDoor(door) { unlockDoor(door).then(() => { door.handle.turn(...


6

First solution using .forEach, for...in and .sort Here what I did to achieve this: First sort your array of objects, i.e convert: data = [ {...}, {...}, {...}, {...}, {...}, {...} ] into an object sorted by year: sortedY = { 2017: [{...}, {...}, {...}], 2018: [{...}, {...}, {...}] } You can achieve such result by using a forEach loop: const sortedY = {...


5

For small arrays as input your script's performance is very good. Let me show you a different approach which might especially improve readability and might also increase performance for larger inputs and multiple runs. .reduce() Instead of iterating through the elements manually, I've used Array.prototype.reduce(): The reduce() method applies a function ...


5

A few points first. Avoid any like the plague. You can nearly always figure out a better type. When dealing with a JSON serialized data, I like to have a function similar to this to get rid of any as soon as possible: function verify<T>(obj: any, fallback: T, isT: (obj: any) => obj is T): T { return isT(obj) ? obj : fallback; } Object.keys and ...


5

One of the improvements would be to use an HttpClient instead of Http for making the API calls. If you use HttpClient, you won't have to call the res.json() everytime you subscribe to the response. To use HttpClient, you'll first have to import HttpClientModule from @angular/common/http. Then you'll have to add it to the imports array of your @NgModule ...


5

One of the main ways in which you can improve this is by using Renderer2 in the directive instead of directly accessing this.el.nativeElement and making changes to it. This might work in most of the cases. But remember, the Angular code might run at other places as well(like in case of Service Workers and in case of Server Side Rendering). In these cases, ...


4

Using prototype is useful for polymorphism. If you want to create another class, eg. ColourfulPoint, you can use this code: ColourfulPoint.prototype = Object.create(Point.prototype); to make your new class inherit all the methods of Point. The first method (using only a constructor) is a way faster (at least in Firefox and Chrome) than the prototype one. ...


4

Building on @RichardJohnson's answer: why not write one function that can filter any array of possibly-undefined values? Let's make it generic. export function filterUndef<T>(ts: (T | undefined)[]): T[] { return ts.filter((t: T | undefined): t is T => !!t) } This simplifies your filter call, too: const nums = [20, 44, undefined] const numsSafe =...


4

As for the architecture it seems good to me. As for the TypeScript I have a few minor suggestions: Avoid using var, use const or let instead. function randomIntString(min: number, max: number): string { const chance = require('chance').Chance(); const randomVar: number = chance.integer({ min: min, max: max }); return randomVar....


4

This is quite a nice idea for a learning project. I created a jsfiddle to test your code. Initially I was not able to run your program, as there were a few minor bugs, most relevant: When you use element.addEventListener, the this context of your App instance is not captured, even though you are using an arrow function. This leads to this.searchIndex being ...


4

The glaring error here is that you use a weird mix of classes and private globals. What happens when someone uses two CardDeck objects simultaneously? Also, as others have said, your code is overkill (there is very little point in having a Card class or an injected shuffler, for example), which makes it hard to read. Really, this could be as simple as ...


4

I found this post because I needed something that did what you implemented. I tried to use the code and ran into some nasty routing bugs whereby Angular was experiencing internal errors. The cause is this bit: // Get the value of all the BehaviorSubject observables in the route so that // the values can be directly accessed from the route ...


4

Only a couple of beautifying changes i would make. I hate giant app.modules so i like to reorginize all core imports into core.module and import that into app.module, like described here. under "core feature module" I do now know what the recent angular-cli generates but i like to think ahead about which modules what my application will include. If it will ...


4

Your method already looks good! I would only make a couple small changes to the logic as follows: The if (this._children.length > 0) isn't necessary as the for loop will make the check for you Instead of using a standard for loop, I would prefer a for..of loop. Typescript will transpile this so it will work in any environment I prefer early returns to ...


4

You could just write export const updateWatchlist = (coin: IAsset, list: IAsset[]) => [...list, coin];


4

You can simplify it by using Array.prototype.some method and lazy evaluation of && operator const pageTypes = ['dashboard', 'klantenkaart', 'complexkaart', 'objectkaart', 'collegakaart']; const subTypes = ['overzicht', 'tijdlijn', 'contracten', 'financieel', 'mededelingen']; function isOnPageWithFilter(currentUrl) { return pageTypes.some(x =>...


3

There is one way I can think of improving this. In angular2, components should only handle view logic. The problem isn't there at the moment but could be there when you add more functionalities. Let's say you want to add a recorder for the song that saves the song from time 0 to time 1. You'd have to add that to this component and keep cluttering it. I ...


3

From a short review: I would follow the method names provided at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map they are both shorter and more intuitive. Your method names are too long More particularly, I would change the name of 'getValuestoclone` I would have the set/get/delete method return the Dictionary object for ...


3

My eye is drawn to the number of parameters in your main animation loop function. I'd try to reduce that. Since this function is essentially chaining callback functions and inserting delays, the opportunity I see is to use promises to more elegantly chain the callbacks (readability is one sure benefit).


Only top voted, non community-wiki answers of a minimum length are eligible