HFT Low Latency C++ Interview Question - Template Metaprogramming

Поделиться
HTML-код
  • Опубликовано: 8 окт 2024
  • Most asked Low latency C++ interview question in HFT, Hedge funds and Investment Banks based on the concepts of template metaprogramming.
    If you are absolutely intrigued by this technique then you are meant to be in a trading firm.
    _________________________________________________________________________
    Follow me on Instagram: / shubham__cr7

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

  • @Wswh123
    @Wswh123 Год назад +2

    Thank you for your help.
    Can i ask why for line 15 there must be a ::value at the end? I understand :: is scope resolution, it takes the global's value of value. Hence its taking the struct gcd value?

    • @CodingInterviewPrep
      @CodingInterviewPrep  Год назад +1

      value is a static member of the gcd class. Static variables are accessed using scope resolution operator ::
      Syntax is ClassName::variable
      Hence we do gcd::value

    • @Wswh123
      @Wswh123 Год назад +1

      @@CodingInterviewPrep thank you so much man. Could I also ask
      1. I am coming from a java background, would say I'm quite decent with java hence the OOP, static type, etc. Is there a good book/tutorial/video to recommend for the transition between java -> cpp? Else what book would you recommend to learn cpp please?
      2. Also, as a starting project im trying to build a simple order book in cpp. However there isn't much useful youtube video/website guide to teach. Do you have any recommended resources or anything to build related projects in c++?
      thank you so much

  • @pramodreddy1358
    @pramodreddy1358 Год назад +7

    Just adding 'constexpr' to the normal runtime gcd function would enable us to run this function in compile time. That is the static_assert would work as expected. Any reason that you had not used that approach seems more easy and concise. Just guessing there might be some limitations with the constexpr approach. If there are can you reply a couple scenarious where the 'constexpr' approach would fail?

    • @CodingInterviewPrep
      @CodingInterviewPrep  Год назад +2

      Yes this can be solved using constexpr function but I did it this way because I wanted to teach a new technique. There are a lot of scenarios where a constexpr function can't be used and template metaprogramming is the only solution.
      Look at the implementation of std::is_same, std::is_class, std::is_pointer etc. on cppreference and then think if you can implement those using constexpr functions. I don't think you can. There are many type traits like that present in header which can only be implemented using a template metafunction.
      Also in some it is easier to write a template metafunction as compared to a constexpr function. For instance this question ruclips.net/video/snIcfIrHSeI/видео.html

    • @CodingInterviewPrep
      @CodingInterviewPrep  Год назад +2

      Watch my latest video: ruclips.net/video/LjfsNyomtro/видео.html
      Do you think you can solve this question using a constexpr function? I don't think you can. Template metaprogramming is a very powerful technique.

    • @aniketbisht2823
      @aniketbisht2823 8 месяцев назад

      @@CodingInterviewPrep You should use constexpr for computation involving only values. If types are involved in any way, only then you might need to use type traits and these sort of template techniques. Not to mention that template instantiation caused by these techniques leads to very slow compile times and therefore does not scale.
      Constexpr has become really capable, you can also allocate memory on the "heap" (C++20) and also use the entirety of (C++23). And when reflection becomes part of the standard (very likely in C++26) then these techniques would be made completely redundant and templates would only be used for Generic Programming (as they originally were intended too).

  • @romokdas8494
    @romokdas8494 2 года назад +6

    If the values of a and b are supplied from the user the compiler won't know the gcd at compile time. So the compile time results can only happen if the input values are constant. If the input values are constant why can't it be precomputed and used directly in our code?

    • @CodingInterviewPrep
      @CodingInterviewPrep  2 года назад +8

      You are missing the damn point. Template metaprogramming is a technique and it can be applied everywhere where recursion is involved. It is used to write code for complex dynamic programming problems where precomputing the result and storing it in the code will be a pain and also increase the binary size. Input might change and someone might forget to update the precomputed result and BOOM your code breaks in production. This happens a lot when new hires/interns make their way into the company as they are not aware about the systems end-to-end. If they forget to update the precomputed result then it wasn't their mistake. It was the mistake of the developer who came up with this idea of storing precomputed results. You gotta fire him. Think about it this way that why do we create macros and constant variables in C++ when we could just use hardcoded value everywhere in the program. Answer: maintainability, readability, flexibility etc.
      There are a shit load of other benefits of Template Metaprogramming.
      It also allows library developers to create ultra low latency libraries.

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

      @@CodingInterviewPrep thanks for the explanation , makes sense now👍👍

    • @Schwein41
      @Schwein41 18 дней назад

      ​@@CodingInterviewPrep why so vulgar damn hahaha

  • @KillerMZE
    @KillerMZE 11 месяцев назад

    const variables actually take memory, and constexpr is more like a macro where the compiler will just replace it. As for actual performance, this isn't very useful in real applications where things aren't known in advance

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

      Do you work in HFT? This is very useful because you want to reduce latency at runtime as much as possible. Saying its not useful when thing aren't known in advance is like saying a car is useless when it comes to flying,. A car has its uses and a plane has its uses.

  • @thewelder3538
    @thewelder3538 3 месяца назад +2

    Wow, if there was a reason not to EVER use Indian coders for ANYTHING, especially mission critical stuff, this is the perfect demonstration.
    I did kind of expect the code to be crap and you definitely didn't disappoint. I mean, the very first code you put on the screen for GCD used a recursive function in a video about low latency. I mean, what's going on in that head of yours? Do you not know about stackframes?
    Then you show some ridiculous template using constexpr like it's some kind of programming genius. When the reality is, this can ONLY be constexpr if your values are KNOWN at compile time. And if you KNOW the values at compile time, you might as well NOT use a calculation at all! Your template has no meaning if the value you're trying to compute the GCD for isn't known until runtime. Oh and NO-ONE writing fast code uses the modulo operator. It's a division, the slowest thing a CPU can do and should be avoided at all cost. You're exactly the kind of coder who if they have determine if a number is odd or even would % 2 rather than & 1.
    Beyond ridiculous video.

    • @mushtaqueahamed1851
      @mushtaqueahamed1851 8 дней назад

      You are missing the point using templates, which is compile time polymorphism. This gets rid of all the crap object oriented programming brings such as virtual functions. Asking compiler to do more work generate machine code for each usage of function specialized for that situation, allows us to avoid the same work to be done at runtime (potentially repeatedly). For example my_function(...), and my_function(...), is better than SomeIntObject.my_function() and SomeDecimalObject.my_function(), where both derive from virtual SomeObject.my_function().
      constexpr and `if constexpr` are more techniques that allow compiler to do more work, so that runtime is spared. Especially when you know already what all possibles types your application will ever have. This allows code that is CPU instruction cache and data cache friendly, help avoid branching and unnecessary function calls etc.
      Point is not that gcd calculated this way is the best way. But, when all this program is ever going to run on known set of inputs, you might as well just let compiler hard code the results into binary, better yet optimize it way as we are not using it at runtime. This hard coding is what is achieved by using all that compiler keywords.
      inline => avoid branching into function call, just duplicate the code within current function, because function call jump is expensive, instruction cache miss etc
      static => initialize the value once for the lifetime of program and reuse it, just keep that data around for next time use, hard coding
      constexpr => better yet, initialize it compile time itself, fail to compile if user did not provide everything needed for this at compile time
      template => i know what all types i will be using later, so generate duplicate code for each type i will use later to avoid runtime type checks even if the resulting binary is large.
      static_assert => just check this at compile time and fail to compile if not, just don't emit any machine code for this check for runtime
      More work done at compile time => Less work to do at runtime.
      This is where LATENCY comes into the picture.

  • @JimmyKal
    @JimmyKal 11 месяцев назад

    awesome video man, actually u don't need to be on HFT to learn this stuff, this can give u foundations to move your C++ knowledge to the next level

  • @mostlysanetrader
    @mostlysanetrader 22 дня назад

    How to get into this as a mechanical engg

  • @MrGoose-fr1wp
    @MrGoose-fr1wp 5 месяцев назад

    Do they face consequence for anything?

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

    Does the quant companies use GCC or clang as a compiler? Are there any benefits of using either one?

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

      GCC and Clang both are widely used. Some firms might be using a lot of other compilers as well like Intel ICC compiler

  • @maYank-nz3dt
    @maYank-nz3dt Год назад

    please make video on full roadmap to become quant dev after 12th

  • @Wan_Destroyer
    @Wan_Destroyer Год назад +1

    Hey i have a question, can i simulate latency or packet loss with this? When yes can u tell me?

    • @CodingInterviewPrep
      @CodingInterviewPrep  Год назад +1

      I am sorry. I didn't understand this question. Could you please elaborate

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

      @@CodingInterviewPrep yes, i would to know how i can simulate a network like i want to do a network emulator, with zhis i can simulate in a network Bandwidth and packet loss, can u do this or is this video wrong for it?

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

      This video is not for your problem.

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

      @@CodingInterviewPrep ok thanks

  • @SachinKumar-vi2dq
    @SachinKumar-vi2dq 2 года назад +1

    Sir just wanted to ask you what resources should be used for being expert in C++. Also tell us how to become a good competitive programmer.
    Also if you don't mind tell us about your internship.

    • @CodingInterviewPrep
      @CodingInterviewPrep  2 года назад +2

      ruclips.net/video/V-VZCtzYybg/видео.html

    • @CodingInterviewPrep
      @CodingInterviewPrep  2 года назад +3

      Don't call me Sir

    • @SachinKumar-vi2dq
      @SachinKumar-vi2dq 2 года назад +1

      @@CodingInterviewPrep thanks shubham bhaiya.
      To big tech firms like MAANG or HFTs open up for electrical and electronics engineering students for SDE roles.
      Please reply

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

    Please if you can guide properly as what can be a potential roadmap to get placed at these companies

    • @CodingInterviewPrep
      @CodingInterviewPrep  2 года назад +2

      "If I will wait for direction my lifetime will pass me by." - Celldweller (Lost in time)
      There's no roadmap. In my earlier videos I have listed all the resources for passing Quant/Core engineer interviews of trading firms. Study them, practice a lot of questions and try to pass the interview. Don't give up in just 2-3 rejections. That is common for most of the people.

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

    Hello, I am from tier 2 cllg and currently working as an SDE, can u pls guide how can I get a quant role in HFT firm?

    • @CodingInterviewPrep
      @CodingInterviewPrep  Год назад +1

      Since you do not have any experience in trading you cannot directly switch into a Quant role. If you are working in Google, Meta or a likewise firm as an ML engineer or in their core infrastructure team then you might have a chance but otherwise no.
      Although you can get a job in Core Engineering division of trading firms. After that you can try to switch into quant role

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

    Isn’t there some way for LLVM to directly do this? I think it’s a optimisation that you can enable on regular programs somehow.
    I wrote the source code for factorials in Rust, and the result was hard coded into the binary. It isn’t template meta programming but it achieves the same result, right…

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

    Awesome stuff bro. Please make more such videos

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

    Can I become a quant if I have a ms in finance from uk ,can I get jobs from big global companies?

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

      Veryyyyyy hard!!! Try for some trading roles actually in Europe itself!! Also curious to know why do you wanna do masters in UK and comeback?

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

      @@aashishc5287 i don't wanna come back ,I'm in my pre final year from a tier 3 engineering college . was planning to do masters in finance as I'm genuinely interested in it but idk the right way to get into quants so I was asking this question btw can i connect with you some how?

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

      @@jackcapital7426 masters in finance isn't a degree to get into Quantitative finance
      You need to get masters in financial engineering or financial mathematics
      You have such programs in Imperial College London as well as Oxford
      You'll need to put lots of efforts in GRE and other skills to get your profile shortlisted for such programs because you mentioned that you are from tier 3 college
      All the best and do let me know if you have other queries

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

      @@aashishc5287 any way to connect in private? Have tons of doubts here plz it will help me to develop my career

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

      @@jackcapital7426 Instagram same username

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

    Bring more videos