Ruby/Rails/Rspec keywords

2021-09-04 00:00:00 +0000

Rspec

  • xdescribe
  • shared_examples
  • expect { }.to raise_error
  • it_behaves_like
  • create_list
  • raise_error
  • expect_any_instance_of
  • have_key
  • be_nil
  • be_successful
  • include
  • shared_context

Rake

  • task
  • TaskArguments.new
  • Task[].execute

Rails

  • ”«-SQL” block
  • around_action/before_action

ActiveRecord

  • .connection.execute
  • .update!
  • .find_by
  • .pluck

Ruby

  • raise/rescue
  • .to_i
  • .uniq
  • .present?
  • .nil?
  • .blank?
  • .to_h
  • .deep_symbolize_keys
  • .map(&:func)
  • .map(&:to_sym)
  • .merge
  • .with_indifferent_access

def print_phrase(&block)
  block.call
end

# the methods in ruby can take only one block
print_phrase { puts "Hello from block!" }

def print_phrase(amount, &block)
  amount.times.each do |x|
    yield
  end 
end 

def print_phrase
  puts yield 
end

print_phrase { "Example phrase" }

On CSS Flexbox

2021-09-03 00:00:00 +0000

  • The flexbox layout is direction-agnostic as opposed to the regular layouts (block which is vertically-based and inline which is horizontally-based).
  • Flexbox layout is most appropriate to the components of an application, and small-scale layouts, while the Grid layout is intended for larger scale layouts.
  • Replaces float and positioning in most cases

Concepts

  • main axis: The main axis of a flex container is the primary axis along which flex items are laid out. Beware, it is not necessarily horizontal; it depends on the flex-direction property
  • main size: A flex item’s width or height, whichever is in the main dimension, is the item’s main size. The flex item’s main size property is either the ‘width’ or ‘height’ property, whichever is in the main dimension.
  • main-start and main-end: The flex items are placed within the container starting from main-start and going to main-end.
  • Similar properties for “cross” instead of “main”

Properties for the Parent (flex container)

  • display: This defines a flex container; inline or block depending on the given value. It enables a flex context for all its direct children,i.e., display: flex;
  • flex-direction: This establishes the main-axis, thus defining the direction flex items are placed in the flex container. Flexbox is (aside from optional wrapping) a single-direction layout concept. Default: row
  • flex-wrap: By default, flex items will all try to fit onto one line. You can change that and allow the items to wrap as needed with this property.
    • nowrap (default): all flex items will be on one line
    • wrap: flex items will wrap onto multiple lines, from top to bottom.
  • justify-content
    • flex-start (default): items are packed toward the start of the flex-direction
    • left: items are packed toward left edge of the container, unless that doesn’t make sense with the flex-direction, then it behaves like start.
    • start: items are packed toward the start of the writing-mode direction.
    • center: items are centered along the line
    • space-between: items are evenly distributed in the line; first item is on the start line, last item on the end line
    • space-around:
    • space-evenly: items are distributed so that the spacing between any two items (and the space to the edges) is equal.
  • align-items: This defines the default behavior for how flex items are laid out along the cross axis on the current line. Think of it as the justify-content version for the cross-axis (perpendicular to the main-axis).
  • align-content: This aligns a flex container’s lines within when there is extra space in the cross-axis, similar to how justify-content aligns individual items within the main-axis.

Properties for the Children (flex items)

  • flex-shrink: by default will allow item to shrink when the container is nowrap and cannot fit items on a single line
  • flex-basis: auto — in this case the browser looks to see if the items have a size. If the items don’t have a size then the content’s size is used as the flex-basis
  • flex-grow: flex items can grow along the main axis from their flex-basis. This will cause the item to stretch and take up any available space on that axis, or a proportion of the available space if other items are allowed to grow too.
  • flex: This is the shorthand for flex-grow, flex-shrink and flex-basis combined. Most common format you will see
    • Setting flex: initial resets the item to the initial values of Flexbox. This is the same as flex: 0 1 auto. In this case the value of flex-grow is 0, so items will not grow larger than their flex-basis size. The value of flex-shrink is 1, so items can shrink if they need to rather than overflowing. The value of flex-basis is auto. Items will either use any size set on the item in the main dimension, or they will get their size from the content size.
    • Using flex: auto is the same as using flex: 1 1 auto; everything is as with flex:initial but in this case the items can grow and fill the container as well as shrink if required.
    • Using flex: none will create fully inflexible flex items. It is as if you wrote flex: 0 0 auto. The items cannot grow or shrink but will be laid out using flexbox with a flex-basis of auto.

React/JS notes

2021-08-15 00:00:00 +0000

useMemo

Motification: cache the expesive computation executed at rendering. Note that it is still a function at rendering time, so starndard limitations, e.g., side-effect, apply

Reduce re-rendering

cache the funcational component, so that when the parent components changes but the props to the child remains same, we useMemo on the child so it does not re-render in this case. Note comparing the props is by reference only

However, if the prop has a callback function passed in, most likely, this function will be regenerated when the parent re-rendered (even with the the same params). In this case, we can put the callback in the useCallback to prevent child re-rendering

How does react render works when the state changes

When render is called, it returns a new virtual DOM. We compute which real dom is changed by diffing the virutal dom recursively (tag, data, and children properties), mark the changed node as dirty, and then render the real dom

Async-await

  • On seeing await, the control will be yielded back to the caller of await. Without await, async will run synchrounously
  • Async functions always return a promise. If the return value of an async function is not explicitly a promise, it will be implicitly wrapped in a promise.

Context

Motiviation: share global data amount component tree nodes

If you only want to avoid passing some props through many levels, component composition is often a simpler solution than context, but moving more complexity higher in the tree makes those higher-level components more complicated and forces the lower-level components to be more flexible than you may want.

React.createContext: When React renders a component that subscribes to this Context object it will read the current context value from the closest matching Provider above it in the tree.

The defaultValue argument is only used when a component does not have a matching Provider above it in the tree. This default value can be helpful for testing components in isolation without wrapping them.

useEffect

Common side effects

  • Mutations
  • subscriptions
  • timers
  • logging
  • async tasks. By definition, this should/does not block the browser from updating the screen

The function passed to useEffect is going to be different on every render. This is intentional. In fact, this is what lets us read the count value from inside the effect without worrying about it getting stale. Every time we re-render, we schedule a different effect, replacing the previous one.

JS keywords

  • Window.localStorage

Good company cultures I can reuse

2021-02-22 00:00:00 +0000

Amazon’s Leadership Principles

  • Although leaders pay attention to competitors, they obsess over customers
  • They act on behalf of the entire company, beyond just their own team. They never say “that’s not my job.”
  • As we do new things, we accept that we may be misunderstood for long periods of time.
  • They seek diverse perspectives and work to disconfirm their beliefs.
  • Many decisions and actions are reversible and do not need extensive study. We value calculated risk taking.
  • They are vocally self-critical, even when doing so is awkward or embarrassing
  • are skeptical when metrics and anecdote differ. No task is beneath them

Netflix Culture

  • You only say things about fellow employees you say to their face
  • We have no bell curves or rankings or quotas such as “cut the bottom 10% every year.” That would be detrimental to fostering collaboration, and is a simplistic, rules-based approach we would never support. We focus on managers’ judgment through the “keeper test” for each of their people: if one of the members of the team was thinking of leaving for another firm, would the manager try hard to keep them from leaving? Those who do not pass the keeper test (i.e. their manager would not fight to keep them) are promptly and respectfully given a generous severance package so we can find someone for that position that makes us an even better dream team
  • Ultimately, your economic security is based on your skills and reputation, not on your seniority at one company. At Netflix, you learn a lot working on hard problems with amazing colleagues, and what you learn increases your market value. Knowing that other companies would quickly hire you if you left Netflix is comforting. We see occasional outside interviewing as healthy, and encourage employees to talk with their managers about what they learn in the process.
  • Picking up the trash is the metaphor for taking care of problems, small and large, and never thinking “that’s not my job.” We don’t have rules about picking up the real or metaphoric trash. We try to create a sense of ownership so that this behavior comes naturally.

Sorted set problems

2021-02-16 00:00:00 +0000

On bridge and cut vertices

2021-02-04 00:00:00 +0000

Example problems

On Eulerian path/cycle

2021-02-03 00:00:00 +0000

On elevator pitch

2021-01-25 00:00:00 +0000

Dos and don’ts

  • Goal is to attract more attenion, not convincing people to hire you upfront
  • Around 30 secs ~ 80 words. No need to recap the entire history. Focus on what matters most
  • SLOW DOWN. To fix it, record your speech, but avoid the trap of over-practice. That makes it sound unnatural
  • Structure
    • Who you are, and your background
    • What you do, and your unique selling propostition, i.e., what can you bring to the team
    • What you want to do/Your goals. What you are looking for
  • Omit negative/dislike areas
  • Use terms based on target audience. Use different pitches for different audiences

My technical elevator pitch

As a backend engineer, I have architected and maintained systems that serve > 20m users and > 1k TPS. I was part of the team that build the leading payment app in Japan from the very beginning, and my code processes billions of dollars each month. I am also the system architect that increased the throughput of this app by a factor of 3. I am excited to work with top engineers to deliver new impacts in the fintech domain or with high traffic systems.

How to answer behavioral questions

2021-01-25 00:00:00 +0000

STAR Method

  • One or two sentences per STAR. Easy to have too long/detailed answers when you are stressed
  • 45-60 secs/120-150 words answer. Too short it doesn’t cover enough details. Too long it becomes rambling
  • After the high level STAR, ask the interviewer if need to provide more details
  • Situtation: Scene and details of example. Make things as measurable as possible, as the interviewer lacks the context of your mind image.
  • Task/Goal/Role: your role/job/goal in the situation. Don’t mistake this for Action
  • Action: What YOU did to achieve the goal. Not what YOUR TEAM did, or what you WOULD do.
  • Result: need to emphaize on the (ideally measurable) impact of your action. Often this was not done enough by candidates
  • Focus on facts and avoid comment/conclusions. If you really need to include comment/conclusions, use the eval from the third party, not yourself.

Tell me about a time you took a risk and failed.

  • Defind failure definition upfront: not meeting expectation/goal/highest standards(maybe)
  • Try not to spend too much time setting the stage, and get to the punch line quickly
  • Start with the situation, and explain why it was challenging. Then go into what you specifically did to try and rectify it
  • Wrap up with your lessons learned, ideally with postive impacts of this learning to future events

Tell me about a mistake you’ve made.

  • How did you handle it?

Tell me a time where you disagreed with your manager

  • Disagree and commit. the bulk of your answer should be talking about how you delivered successfully in spite of not being on board with the initial decision
  • Decision was made after conflict, doesn’t matter which side won
  • Be clear about what you don’t agree with and offer alternatives
  • When I disagree and was wrong, it is because of unknown unkowns. Known unkowns would mean i didn’t do due diligence
  • Would be nice to define “right/wrong” but probably too much detail to cover in 1 min

Describe a time when you struggled to build a relationship with someone important.

  • How did you eventually overcome that?

Tell me about a time you were under a lot of pressure.

  • What was going on, and how did you get through it?

Give me an example of a time when you were able to successfully persuade someone to see things your way at work.

Describe a time when you saw some problem and took the initiative to correct it rather than waiting for someone else to do it.

Tell me a time you had received a negative feedback

  • Explain a situation where you improved your performance after receiving constructive criticism.
  • Talk about how you listened and made changes when a boss or client critiqued your work.
  • It can also be insightful to describe a time when your work was unfairly criticized, and how you were able to rebuff the criticism without making anyone look bad.
  • Do not make the mistake of going into excessive detail about any work slip-ups—instead, focus on your positive reaction to criticism.

Editorial: LC premium set 4

2020-12-21 00:00:00 +0000

Editorial: LC premium set 3

2020-12-13 00:00:00 +0000

Editorial: LC premium set 2

2020-12-06 00:00:00 +0000

Editorial: LC premium set 1

2020-12-01 00:00:00 +0000

On FFT

2020-11-15 00:00:00 +0000

Original video

  • Eval a polynomical A(x) at x0 : Horner’s rule
  • Mutlply two polys:
    • Closed form at O(n^2) naively -> O(nlogn) possible
    • Equivalent to convolution of vectors A * reverse(B) - inner products of all possible shifts
  • Poly representations:
    • Samples for n points with all distince xs.
      • Convert between this and coefficient form in O(nlogn)
      • Sampling n points takes O(n^2) in total
    • A sequence of n roots
  • Eval A(x) for x in X: D&C by even/odd i
    • A(x) = A_even(x^2) + x * A_odd(x^2)
    • Derive the recursion and cost => draw the recursion tree and we see it is still n^2
    • Square a number on the unit circle on the complex plane
  • FFT is the d&c algo for DFT (coefficent to sample form)
    • Fast poly multiplicatin: FFT(A) * FFA(B) and then do inverse FFT
  • Similar techique

Editorial: Coding set 18

2020-10-25 00:00:00 +0000

Editorial: Coding set 17

2020-10-11 00:00:00 +0000

Editorial: Coding set 16

2020-09-28 00:00:00 +0000

Editorial: Coding set 15

2020-09-19 00:00:00 +0000

Editorial: Coding set 14

2020-09-03 00:00:00 +0000

Good for whiteboarding

Champagne Tower

  • My first attempt was wrong

Number of Subarrays with Bounded Maximum

  • Corner case

988. Smallest String Starting From Leaf

  • My optimization was wrong

Additive Number

  • First attempt missed a corner case

Super Pow

  • My gut feeling was wrong

Longest Word in Dictionary through Deleting

  • Official insight is cleaner than mine
  • First attempt missed a corner case

Editorial: Coding set 13

2020-08-22 00:00:00 +0000

Good for whiteboarding

Iterator for Combination

  • My optimization was incorrect