Skip to the editor
JSGroundwork
JSGroundwork handwritten · web dev
←Back to Matrix problems

Ready to read

JSJavaScript⑂Git◎Interview prepΣDSA in JSSDSystem Design
More topics15›
</>HTML{ }CSS⚛ReactNNext.jsNeNest.jsTSTypeScriptNoNode.js🐳DockerDBSQL & Databases✓Testing🔒Web Security☁Cloud & DevOps◈GraphQL◆Redis☸Kubernetes
☰‹›
Description
All topics›JavaScript›Matrix problems

Search a 2D Matrix II

advancedlayer I12 · Matrix problems5 tests

Search a value in an m × n matrix where every row is sorted left to right and every column is sorted top to bottom. Return true if target is present.

  • The rows are not one long sorted sequence — a row can start lower than the previous row ended, so a plain binary search over the flattened matrix is wrong
  • Aim for O(m + n) time and O(1) space
  • An empty matrix returns false

Stuck?

All exercisesPlayground
1234567891011121314
loading the editor…

Nothing yet — hit Run and whatever you log shows up here.

Submit to see how you did.

⌘/Ctrl + Enter run⌘/Ctrl + S save⌘/Ctrl + / comment⌘/Ctrl + F find/replace⌘/Ctrl + D select next matchAlt + click multi-cursorTab indent · ⇧Tab outdentEsc leave fullscreen