LeetCode Medium 180 "Consecutive Numbers" Amazon Uber Interview SQL Question Detailed Explanation

Поделиться
HTML-код
  • Опубликовано: 3 дек 2024

Комментарии • 33

  • @EverydayDataScience
    @EverydayDataScience  4 месяца назад

    Working solution:
    WITH cte AS
    (SELECT *, ROW_NUMBER() OVER(PARTITION BY num ORDER BY id)
    AS rnk
    FROM Logs),
    cte2 AS
    (SELECT *, (id*1.0-rnk*1.0) AS diff. // multiplication with 1.0 is done so that SIGNED/UNSIGNED difference error doesn't occur
    FROM cte)
    SELECT DISTINCT num AS ConsecutiveNums
    FROM cte2
    GROUP BY diff, num
    HAVING COUNT(*) >= 3;

  • @aspirationqueen
    @aspirationqueen 5 месяцев назад +6

    when id's are jumbled up and id's don't follow sequence use this query with cte as
    (
    select id, num ,lead(num,1) over(order by id) as next, lead(num,2) over(order by id ) as next_2_next,
    lead(id,1) over(order by id) as next_team_id, lead(id,2) over(order by id) as next_2_next_team_id
    from Logs
    )
    select distinct num as ConsecutiveNums
    from cte
    where (num=next and num=next_2_next)
    and (id+1=next_team_id and id+2=next_2_next_team_id)

    • @supriyakushwaha21
      @supriyakushwaha21 5 месяцев назад

      I WAS WONDERING WHY THE CODE IN THIS VIDEO IS NOT WORKING. BUTY, AFTER WRITING YOUR CODE , ALL TESTCASES PASSED. THANKS

  • @saisaketh8433
    @saisaketh8433 Год назад

    These videos are very helpful in learning sql!!!!

  • @Thevisionaryaddy
    @Thevisionaryaddy 3 месяца назад

    loved the solution , thanks

  • @june17you
    @june17you 4 месяца назад +2

    Hi Bro, The given solution is not working currently. Not sure if test cases are updated. Kindly help us if there is any other easy approach to solve this. Thank you

  • @kelvinlow823
    @kelvinlow823 Год назад

    brilliant solution thanks

  • @oliver_comp
    @oliver_comp Год назад

    explanation top 100%

  • @gowtham4383
    @gowtham4383 Год назад

    Great. Thanks

  • @PiyushKumar-tv6dr
    @PiyushKumar-tv6dr 6 месяцев назад

    for sql server? we can't mention inside the argument in lead function

  • @RajeshKumar._.16
    @RajeshKumar._.16 Год назад

    nice

  • @digitaldruglord7232
    @digitaldruglord7232 7 месяцев назад

    perfectly explained, Thnx

  • @KshitijSoni-kn2gp
    @KshitijSoni-kn2gp 6 месяцев назад +3

    This won't work when Id's are jumbled up

    • @aspirationqueen
      @aspirationqueen 5 месяцев назад +1

      with cte as
      (
      select id,num,lead(num,1) over(order by id) as next,lead(num,2) over(order by id ) as next_2_next,
      lead(id,1) over(order by id) as next_team_id,lead(id,2) over(order by id) as next_2_next_team_id
      from Logs
      )
      select distinct num as ConsecutiveNums
      from cte
      where (num=next and num=next_2_next)
      and (id+1=next_team_id and id+2=next_2_next_team_id) can use this one

    • @EverydayDataScience
      @EverydayDataScience  4 месяца назад

      WITH cte AS
      (SELECT *, ROW_NUMBER() OVER(PARTITION BY num ORDER BY id)
      AS rnk
      FROM Logs),
      cte2 AS
      (SELECT *, (id*1.0-rnk*1.0) AS diff. // multiplication with 1.0 is done so that SIGNED/UNSIGNED difference error doesn't occur
      FROM cte)
      SELECT DISTINCT num AS ConsecutiveNums
      FROM cte2
      GROUP BY diff, num
      HAVING COUNT(*) >= 3;

  • @rupdeepthey
    @rupdeepthey Год назад

    Awesome !!

  • @callmeruks
    @callmeruks 2 года назад

    Great Explanation

    • @EverydayDataScience
      @EverydayDataScience  2 года назад +1

      Glad that you found it useful, Rukshar.

    • @callmeruks
      @callmeruks 2 года назад

      @@EverydayDataScience Can you make a video on Tree node -problem 608

  • @朱怡蓁-u9y
    @朱怡蓁-u9y Год назад +1

    why can't we write where num = next_1 = next_2? Thank you!

    • @rupdeepthey
      @rupdeepthey Год назад

      Fails for negative nums idk why.

  • @atharvaathalye
    @atharvaathalye 4 месяца назад

    test case with 4 1's are not accepting.

    • @EverydayDataScience
      @EverydayDataScience  4 месяца назад +1

      WITH cte AS
      (SELECT *, ROW_NUMBER() OVER(PARTITION BY num ORDER BY id)
      AS rnk
      FROM Logs),
      cte2 AS
      (SELECT *, (id*1.0-rnk*1.0) AS diff. // multiplication with 1.0 is done so that SIGNED/UNSIGNED difference error doesn't occur
      FROM cte)
      SELECT DISTINCT num AS ConsecutiveNums
      FROM cte2
      GROUP BY diff, num
      HAVING COUNT(*) >= 3;

  • @ce_070_sunny_nath8
    @ce_070_sunny_nath8 Год назад

    Sir please update the solution since question is telling that atleast three so there can be many more occurrences for same no.

    • @aakashmittal8598
      @aakashmittal8598 Год назад

      use DISTINCT in SELECT statement:
      WITH cte AS(
      SELECT *, LEAD(num,1) OVER() AS NEXT_1,
      LEAD(num,2) OVER() AS NEXT_2
      FROM Logs)
      SELECT DISTINCT num AS ConsecutiveNums
      FROM cte
      WHERE num = NEXT_1 AND
      num = NEXT_2;

    • @EverydayDataScience
      @EverydayDataScience  4 месяца назад

      WITH cte AS
      (SELECT *, ROW_NUMBER() OVER(PARTITION BY num ORDER BY id)
      AS rnk
      FROM Logs),
      cte2 AS
      (SELECT *, (id*1.0-rnk*1.0) AS diff. // multiplication with 1.0 is done so that SIGNED/UNSIGNED difference error doesn't occur
      FROM cte)
      SELECT DISTINCT num AS ConsecutiveNums
      FROM cte2
      GROUP BY diff, num
      HAVING COUNT(*) >= 3;

  • @massvinod8413
    @massvinod8413 4 месяца назад

    This solution is not working for some test cases. Please check it once .

    • @EverydayDataScience
      @EverydayDataScience  4 месяца назад

      WITH cte AS
      (SELECT *, ROW_NUMBER() OVER(PARTITION BY num ORDER BY id)
      AS rnk
      FROM Logs),
      cte2 AS
      (SELECT *, (id*1.0-rnk*1.0) AS diff. // multiplication with 1.0 is done so that SIGNED/UNSIGNED difference error doesn't occur
      FROM cte)
      SELECT DISTINCT num AS ConsecutiveNums
      FROM cte2
      GROUP BY diff, num
      HAVING COUNT(*) >= 3;

  • @rayyanamir8560
    @rayyanamir8560 2 года назад

    Thanks sir for such a great explanation. What's your paypal?

  • @AdarshChikkaswamygowdaAdarsh
    @AdarshChikkaswamygowdaAdarsh Год назад

  • @amanraheja2905
    @amanraheja2905 Год назад

    brilliant solution thanks