# DGM Interview with Nadya ### Determine whether a string contains a backwards copy of another string. ```typescript const containsBackwards (a: string, b: string): boolean => ; ``` #### Test Cases ```javascript containsBackwards("hello", "leh") === true; containsBackwards("hello", "hel") === false; containsBackwards("hello world", "w o") === true function reverse(var str) { var split = str.split(); var reverseArray = split.reverse(); var joinArray = reverseArray.join(""); var test1 = ("hello world").substr(6, 9); test1.join(" "); return joinArray(); } /* * return whether `a` returns a backwards copy of `b` */ function containsBackwards(var a, var b): boolean { return reverse(b).split().join(""); } ``` ### Fix the bug in this function that converts a list of vectors into a list of points. ```typescript class Vector { constructor (dx, dy) { this.dx = dx; this.dy = dy; } addToPoint(p: Point) { return new Point(p.getX() + this.dx, p.getY() + this.dy); } } /* * Convert a list of vectors into a list of points, * starting from 0,0, and in the same order as the list of vectors */ const makeAbsoluteReverse = (vs: Vector[]): Point[] => vs.reduce(([prev, ...rest]: Point[], v: Vector) => [v.addToPoint(prev), prev, ...rest], [new Point(0,0)]); const makeAbsolute = (vs: Vector[]): Point[] => makeAbsoluteReverse(vs).reverse(); > makeAbsolute([new Vector(5,4), newVector(-4,3)]) [new Point(0,0), new Point(5,4), new Point(1,7)] ``` ```typescript type Vector = { dx: number, dy; number, }; type Point = { x: number, y: number, }; /* * Convert a list of vectors into a list of points, * starting from 0,0, and in the same order as the list of vectors */ const makeAbsolute = (vs: Vector[]): Point[] => vs.reduce(([prev, ...rest]: Point[], v: Vector) => [{ x: prev.x + v.dx, y: prev.y + v.dy, }, prev, ...rest], {x: 0, y: 0}); ``` ### Given an array of numbers, return the greatest number without using sort. ```typescript const greatest = (ns: number[]): number => ; ``` ```javascript function myMax (var test) { return Math.max(test); } arr.length ? Math.max(...arr) : 0 arr.length ? Math.max.apply(null, arr) : 0) Math.max.apply(null, []) => -Infinity myMax([]) myMax(["1", 2]) type NonEmpty<T> = [T, ...T]; function myMax (var test: NonEmpty<numbers>) /*...*/ ``` ### Given an array of numbers, return the second greatest number without using sort. ```javascript function test<T>(arr: [T, T, ...T]) { if (arr.length < 2) { /* do something */ } var max = Math.max.apply(null, arr); arr.splice (arr.indexOf(max), 1); return Math.max.apply(null, arr); } test([0]); test([0,1]); ``` ### What's your preferred way to manage state? * How would you manage state to make undo easy to implement? ```typescript const initialState = /*...*/; let events = []; function recieveEvent(ev){ events.push(ev); /*...*/ } function undo(){ resetToInitialState(); events.pop(); event.forEach(recieveEvent); } ```