Skip to content

[FE] - PHUONG BUI THANH (Michael - btphuong2017@gmail.com)#291

Open
btphuong2017 wants to merge 4 commits into
99techteam:mainfrom
btphuong2017:main
Open

[FE] - PHUONG BUI THANH (Michael - btphuong2017@gmail.com)#291
btphuong2017 wants to merge 4 commits into
99techteam:mainfrom
btphuong2017:main

Conversation

@btphuong2017

Copy link
Copy Markdown

Solutions — 99Tech Code Challenge #1

Problem 1: Three ways to sum to n

Location: src/problem1/index.ts

Three distinct implementations of sum_to_n(n), each returning 1 + 2 + ... + n. For n ≤ 0 all three consistently return 0 (empty sum).

Approach Complexity
A Iterative for loop O(n) time, O(1) space
B Gauss closed form n*(n+1)/2 O(1) time, O(1) space
C Array.from + reduce O(n) time, O(n) space

Implementation B is the practical choice for any real usage. Implementation C demonstrates a functional style that composes naturally with further sequence transforms.


Problem 2: Fancy Form

Location: src/problem2/ — run with npm install && npm run dev

A currency swap form built with Vite + React + TypeScript (bonus stack).

Key features:

  • Fetches live token prices from the Switcheo API on load; deduplicates multiple records per currency by keeping the most recent date entry. Tokens without a price are omitted.
  • Token icons loaded from the Switcheo CDN; missing icons are hidden gracefully.
  • Real-time receive-amount computation: receiveAmount = sendAmount × (fromPrice / toPrice).
  • Swap-direction flip button; inline validation (positive amount, both tokens selected, tokens must differ).
  • Confirm Swap simulates a backend call with a 1.5 s loading spinner → success banner → form reset.

Unit tests for the pure lib/ logic (exchange-rate math, price deduplication/filtering) are in src/lib/*.test.ts and run with npm test.


Problem 3: Messy React

Location: src/problem3/

README.md lists 13 issues found in the original WalletPage component, ordered from most to least severe:

  1. lhsPriority is not defined → runtime ReferenceError (should be balancePriority)
  2. Filter logic is inverted — keeps amount <= 0 instead of > 0
  3. WalletBalance interface missing the blockchain field used in the code
  4. getPriority re-created on every render (should be hoisted to module scope)
  5. blockchain: any parameter discards type safety
  6. prices in useMemo deps but never used by the filter/sort
  7. Sort comparator returns undefined for equal priorities
  8. rows maps over sortedBalances but casts to FormattedWalletBalancebalance.formatted is always undefined
  9. formattedBalances not memoised — runs on every render
  10. Array index used as React key
  11. prices[balance.currency] unguarded → potential NaN
  12. toFixed() called without a precision argument
  13. children destructured but never used

WalletPage.tsx is the refactored version: typed Blockchain union, getPriority hoisted with a never exhaustiveness check, a single useMemo that filters, sorts, and formats in one pass, stable composite keys, and guarded usdValue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant